开发者

how to produce a delay effect

I have a loop with in a loop like this

$.each(requiredCities,function(keya,valb){
 $.each(requiredKeyWords,function(keya,valb){
  开发者_如何学Godownload(keya,valb);
 });
});

I want this download function to be called every 2 seconds. The problem is im unable to use setTimeout.


Do this:

var i=1;
$.each(requiredCities,function(keya,valb){
 $.each(requiredKeyWords,function(keya,valb){
    var timerId = setInterval(function(){
       download(keya,valb);
       clearInterval(timerId);
    }, 2000*(i++));    
 });
});

Cheers


Use the delay method from the jQuery API if you want to waits some time before next call.

Use everyTime method from jQuery Timer Plugin if you want your code runing again continuously after some time (I think it's your case based on your comment).


I find that creating a simple function and calling your code through the function via setInterval instead of setTimeout. It really just depends on what you are trying to do.

perhaps this would work better:

setInterval("fooBar()", 1000);

function fooBar(){
$.each(requiredCities,function(keya,valb){
 $.each(requiredKeyWords,function(keya,valb){
  download(keya,valb);
 });
});
}

not sure if that works, but at first brush I think it would be fine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜