0

He guys,

Maybe you can help me, i need a code when the page is loaded a function (below)

 function update() {
     $('#animation').load('/Stream/readLevel');
 } 

needs to execute, when this action is complete (so the content of the page /Stream/readLevel has been fully loaded the function must be loaded again in a number of seconds (2 seconds).

How can i accomplish this?

Thanks in advance!

5 Answers 5

3

As you only want to reload after the contents has been fully loaded you have to set the timer in success callback of $.load function like following, otherwise it will send request to server every two seconds irrespect of if the previous ajax request has been completed or not.

function update() {
     $('#animation').load('/Stream/readLevel',function(){
        var timer = setTimeout(update,2000);
     });
 } 
$(function(){
   update();
});
Sign up to request clarification or add additional context in comments.

4 Comments

This is so far the only answer that does what the op asked
Somebody should not edit my answer and add contents like that :s
@Adil that's not what the OP wants.. he doesn't want to blindly spam the server every 2 seconds but to send request after 2 seconds of fully loading the previous request..
@MarcoVeenendaal, If this answer works for you, please check the little check-mark next to it.
0
<script>
function update() {
    $("#animation").load('/Stream/readLevel',"",function(){
        setTimeout(update,2000);
    });
}
</script>

<body onload="update()">
  // HTML CODE
</body>

1 Comment

that's not what the OP wants.. he doesn't want to blindly spam the server every 2 seconds but to send request after 2 seconds of fully loading the previous request..
0

Try

function update(){ 
    $('#animation').load('/Stream/readLevel',function(){
    setTimeout(update,2000); });

}

5 Comments

Don't pass strings to setTimeout.
@Somebodyisintrouble That will trigger the evil eval.
So fast Dcoder changed his name to Joy.Iam Impressed.BTW @Joy thanks for answering
Nop, I didn't change my name. :(
that's not what the OP wants.. he doesn't want to blindly spam the server every 2 seconds but to send request after 2 seconds of fully loading the previous request..
0

you can do it like this.

$(function(){    
   update();
});


function update() {
   $('#animation').load('/Stream/readLevel', function(){
       alert("after two seconds of load");
       setTimeout('update()', 2000);
  }); 
}

3 Comments

I've tried this but when the page is not fully loaded no action is performed on my site (this is a piece of code in readLevel code: echo '$("#'.$player_name . '").animate({ width: "'. $player_width . 'px",height: "' . $player_height . 'px",}, 3000 );'; ) so this animate action doesn't work (every 2 seconds)
that's not what the OP wants.. he doesn't want to blindly spam the server every 2 seconds but to send request after 2 seconds of fully loading the previous request..
still not correct, the interval will almost always fire 2 requests before it is cleared. You want to use setTimeout and at that point your answer is just copypaste of the correct one and doesn't contribute anything.
0

var c = 2; function update() { $('#animation').load('/Stream/readLevel'); } function startTimer(){

if(c< 0){
c=2;
}
if(c==2){
update();
}
timer = setTimeout('startTimer()',1000);
c=c-1;
}
$(document).ready(function(){
startTimer();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.