加载javascript函数,当它加载时,在指定的秒数内再次加载它


Load javascript function and when its loaded load it again in a specified number of seconds

伙计们,

也许你可以帮我,当页面加载函数时,我需要代码(如下)

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

需要执行,当此操作完成时(因此页面的内容/Stream/readLevel 已完全加载该函数必须在几秒钟(2 秒)内再次加载。

我怎样才能做到这一点?

提前感谢!

由于您只想在内容完全加载后重新加载,因此您必须在$.load函数的回调中设置计时器success如下所示,否则它将每两秒向服务器发送一次请求,而不考虑上一个 ajax 请求是否已完成。

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

你可以这样做。

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

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

<script>
function update() {
    $("#animation").load('/Stream/readLevel',"",function(){
        setTimeout(update,2000);
    });
}
</script>
<body onload="update()">
  // HTML CODE
</body>

尝试

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

var c = 2; 函数更新() { $('#animation').load('/Stream/readLevel'); } 函数启动计时器(){

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