doniyor
doniyor

Reputation: 37856

change images every 5 seconds - but images from server

i am trying to change the users in every seven seconds. for this, i need to load all information of user from server. how can i do this using javascript? what is the efficient way?

what i thought about is, i send every seven seconds an http request to server asking for user data with ajax. it is efficient way of doing this?

http://doni.sites.djangoeurope.com/ this is my site and in bottom part you see 5 users. i want them to change to another user in every seven seconds.

please help me. i am using serverside django,

Upvotes: 0

Views: 613

Answers (2)

defau1t
defau1t

Reputation: 10619

A simple logic would be to use setInterval function. Lets us assume that I have to change the background of a webpage after every 5 seconds. You could use the below code to achieve this:

<script type="text/javascript">
        function changebackground() {
            var colors = ["#0099cc","#c0c0c0","#587b2e",
"#990000","#000000","#1C8200","#987baa","#464646",
"#AA8971","#1987FC","#99081E"];

            setInterval(function() {
                var bodybgarrayno = Math.floor(Math.random() * colors.length);
                var selectedcolor = colors[bodybgarrayno];
                document.body.style.background = selectedcolor;
            }, 3000);
        }
        </script>

You could replicate the above functionality for your requirements.

DEMO

Upvotes: 1

Filippo oretti
Filippo oretti

Reputation: 49817

the most efficient way is to use websockets , you should document on them.

Only thing is they are not always suppoerted from browsers

You can also do somenthing like:

setInterval(function(){
//launch my ajax every 5 seconds
},5000);

but it do not reccomend you this

Upvotes: 2

Related Questions