admin管理员组文章数量:1426454
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
4 Answers
Reset to default 3for (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
版权声明:本文标题:Run a anonymous function several times using javascript's setTimeout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745477229a2660015.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论