所以,基本上,我有三个功能,我希望它们一个接一个地延迟两秒运行。
我想要实现以下目标:
firstFunction();
// Two seconds delay
secondFunction();
// Two seconds delay
thirdFunction();
// Two seconds delay
firstFunction();
等等。我尝试了setInterval,setTimeout,jquery delay,到目前为止,我什么都没做-在最佳情况下,所有三个功能都同时运行。确切地说,这三个函数的代码非常相似
var active = $(".active.two").removeClass('active');
if (active.next('img') && active.next('img').length) {
active .next('img').addClass('active');
} else {
active.siblings(":first-child").addClass('active');
}
如果您能告诉我正确的方向,我将不胜感激。
您可以使用它setInterval
来实现。通过将对函数的引用存储在数组中,然后依次调用它们,可以使其更简单。试试这个:
function funcOne() {
console.log('one');
}
function funcTwo() {
console.log('two');
}
function funcThree() {
console.log('three');
}
var i = 0;
var funcs = [ funcOne, funcTwo, funcThree ];
setInterval(function() {
funcs[i % funcs.length]();
i++;
}, 2000);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句