admin管理员组

文章数量:1429979

I am using the following function on a website. Excusing the sloppy jQuery I'm using, is there any way to run this function multiple times without declaring it as a standalone function and without copying and pasting the code several times?

Exhibit A:

setTimeout(function(){

    $('.nav > li > a').each(function(k,el){
        var width = $(this).parent().width();
        $(this).next('span').width(width);
    });

},1000);

I don't want to do this:

setTimeout(function(){
        // same code here   
},1000);

setTimeout(function(){
        // same code here   
},3000);

setTimeout(function(){
        // same code here   
},5000);

Nor do I want to do this:

function myfunction{
    // same code here   
}

setTimeout('myFunction()',1000);
setTimeout('myFunction()',3000);
setTimeout('myFunction()',5000);

I am using the following function on a website. Excusing the sloppy jQuery I'm using, is there any way to run this function multiple times without declaring it as a standalone function and without copying and pasting the code several times?

Exhibit A:

setTimeout(function(){

    $('.nav > li > a').each(function(k,el){
        var width = $(this).parent().width();
        $(this).next('span').width(width);
    });

},1000);

I don't want to do this:

setTimeout(function(){
        // same code here   
},1000);

setTimeout(function(){
        // same code here   
},3000);

setTimeout(function(){
        // same code here   
},5000);

Nor do I want to do this:

function myfunction{
    // same code here   
}

setTimeout('myFunction()',1000);
setTimeout('myFunction()',3000);
setTimeout('myFunction()',5000);
Share Improve this question asked Dec 13, 2011 at 19:45 cwdcwd 54.9k55 gold badges171 silver badges199 bronze badges 1
  • Are you wanting it to call itself at the end in a setTimeout (basically run every X seconds) or just n different calls to setTimeout with the same function? – Corbin Commented Dec 13, 2011 at 19:47
Add a ment  | 

4 Answers 4

Reset to default 3
for (var i = 1000; i <= 5000; i += 2000) {
    setTimeout(func, i);
}

or

var times = [1000, 3000, 5000];
for (var i=0; i<times.length; i++) {
    setTimeout(func, times[i]);
}

newer browsers only

[1000, 3000, 5000].forEach(function(time){
    setTimeout(func, time);
});

Yes, use the setInterval method.

setInterval(function() {
    $('.nav > li > a').each(function(k,el){
      var width = $(this).parent().width();
      $(this).next('span').width(width);
    }); 
}, 2000);

//run every two seconds

Alternatively, you can call setTimeout repeatedly...

myFun = function() {
    $('.nav > li > a').each(function(k,el){
      var width = $(this).parent().width();
      $(this).next('span').width(width);
    });
    setTimeout(myFun, 2000);
}

setTimeout(myFun,1000);

This should have the same effect as doing 1000,3000,etc...

You could use $.map

$.map([1000,3000,5000], function(time,index){ 

 setTimeout(function(){
        $('.nav > li > a').each(function(k,el){
        var width = $(this).parent().width();
        $(this).next('span').width(width);
    });
 },time);

}); 
(function(){
    var i, weirdProblem = function(timeout) {
        setTimeout(function() {
            $('.nav > li > a').each(function(){
                var width = $(this).parent().width();
                $(this).next('span').width(width);
            });
        },timeout);
    };
    for(i = 1; i <= 5; i+=2) {
        weirdProblem(i*1000);
    }
})();

The closure encapsulates the declarations and will be garbage collected once no longer used. Not sure the point of this though. Very strange.

本文标签: Run a anonymous function several times using javascript39s setTimeoutStack Overflow