Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用setInterval在x次调用后执行函数_Javascript_Jquery - Fatal编程技术网

Javascript 使用setInterval在x次调用后执行函数

Javascript 使用setInterval在x次调用后执行函数,javascript,jquery,Javascript,Jquery,在中,以下函数用于使用setInterval()执行x次操作 回调()在这里做什么?我尝试在指定的重复次数完成后执行一个函数。但是这个 setIntervalX(function () { animateColor(); }, 3000, 4, function(){ completeFunction(); }); 不起作用。也许这种语法是非常错误的。我的印象是,使用jquery可以将这样的函数串在一起 非常感谢您的洞察力。谢谢 我想你对描述有点误解了。当您希望在迭代后使用回调函数

在中,以下函数用于使用setInterval()执行x次操作

回调()在这里做什么?我尝试在指定的重复次数完成后执行一个函数。但是这个

setIntervalX(function () {
    animateColor();
}, 3000, 4, function(){

completeFunction();

});
不起作用。也许这种语法是非常错误的。我的印象是,使用jquery可以将这样的函数串在一起


非常感谢您的洞察力。谢谢

我想你对描述有点误解了。当您希望在迭代后使用回调函数时,setIntervalX执行交互x次

function setIntervalX(interationFunction, delay, repetitions, callbackFunction) {
    var x = 0;
    var intervalID = window.setInterval(function () {

        iterationFunction();

        if (++x === repetitions) {
            callbackFunction();
            window.clearInterval(intervalID);
        }
    }, delay);
}

setIntervalX(
  function() {
    // this executed every time the interval triggers
  },
  1000, // amount of milliseconds to delay before interval triggers
  5, // amount of repetitions before interval is stopped and callback is executed
  function() {
    // this will be executed after the interval triggered 5 times
    // so after round about 5 seconds after setIntervalX was started
  }
);

我想你对描述有点误解了。当您希望在迭代后使用回调函数时,setIntervalX执行交互x次

function setIntervalX(interationFunction, delay, repetitions, callbackFunction) {
    var x = 0;
    var intervalID = window.setInterval(function () {

        iterationFunction();

        if (++x === repetitions) {
            callbackFunction();
            window.clearInterval(intervalID);
        }
    }, delay);
}

setIntervalX(
  function() {
    // this executed every time the interval triggers
  },
  1000, // amount of milliseconds to delay before interval triggers
  5, // amount of repetitions before interval is stopped and callback is executed
  function() {
    // this will be executed after the interval triggered 5 times
    // so after round about 5 seconds after setIntervalX was started
  }
);
函数setIntervalX(函数、延迟、时间、回调){
如果(次数>0){
setTimeout(函数(){
func.apply(arguments.callee);
setIntervalX(函数,延迟,--次,回调);
},延误);
}否则{
回调。应用(此);
}
}
setIntervalX(函数(){
document.write('ping!
'); },1000,5,函数(){ document.write('Finished!
'); });

我个人更喜欢setTimeout方法。。。但那只是我。试一试,看看是否有效

啊,看来我误读了你的整个帖子。。。所以托比亚斯对你的问题的推理是正确的。我的是您想要的示例。

函数setIntervalX(func、delay、times、callback){
如果(次数>0){
setTimeout(函数(){
func.apply(arguments.callee);
setIntervalX(函数,延迟,--次,回调);
},延误);
}否则{
回调。应用(此);
}
}
setIntervalX(函数(){
document.write('ping!
'); },1000,5,函数(){ document.write('Finished!
'); });

我个人更喜欢setTimeout方法。。。但那只是我。试一试,看看是否有效

啊,看来我误读了你的整个帖子。。。所以托比亚斯对你的问题的推理是正确的。我的就是你想要的一个例子。

哇,更简单的是:

哇,简单多了:


原始函数中的
回调
X次以内的每个间隔上运行。尝试此修改以允许完全回调:

setIntervalX(function () {
    animateColor();
    //or something
}, 3000, 4, function(){
    alert("all done!");
});

function setIntervalX(callback, delay, repetitions, complete) {
    var x = 0;
    var intervalID = window.setInterval(function () {

        callback();

        if (++x === repetitions) {
            window.clearInterval(intervalID);
            complete();
        }
    }, delay);
}

原始函数中的
回调
X次以内的每个间隔上运行。尝试此修改以允许完全回调:

setIntervalX(function () {
    animateColor();
    //or something
}, 3000, 4, function(){
    alert("all done!");
});

function setIntervalX(callback, delay, repetitions, complete) {
    var x = 0;
    var intervalID = window.setInterval(function () {

        callback();

        if (++x === repetitions) {
            window.clearInterval(intervalID);
            complete();
        }
    }, delay);
}

因此,当作者说这个“/”时,每隔1秒重复5次:setIntervalX(函数(){//Your logic here},1000,5);“Your logic here部分并不是指我想重复的函数。但它确实是这样工作的……即使我做错了。你能给我举一个例子,说明如何用你的方式调用它,当它完成重复时,使用回调吗?谢谢我编辑了我的代码示例,并添加了一个示例,其中包含一些解释性的注释。当作者说这个“/”这将每隔1秒钟重复5次:setIntervalX(function(){//Your logic here},1000,5);“Your logic here部分没有提到我要重复的函数。但它确实是这样工作的……即使我做错了。你能给我举一个例子,说明如何用你的方式调用它,当它完成重复时,使用回调吗?谢谢我编辑了我的代码示例,并添加了一个如何调用它的示例和一些解释性注释。我很欣赏这一点,但似乎为这个小函数加载整个新库有点过分。但框架看起来很酷!谢谢非常感谢你的评论。你的右边,帧是为比这更长的序列而设计的。动画越复杂,回调就越复杂。像async和Frame这样的流控制库有助于解决这样的问题,而at 10k通常可以内联使用,对项目没有太大影响。我很欣赏这一点,但似乎仅仅为这个小函数加载整个新库有点过分。但框架看起来很酷!谢谢非常感谢你的评论。你的右边,帧是为比这更长的序列而设计的。动画越复杂,回调就越复杂。像async和Frame这样的流控制库有助于解决这样的问题,而AT10K通常可以内联使用,对项目没有太大影响。Noice!谢谢你能给我举一个使用setTimeout的例子吗?告诉我你为什么喜欢它。如果在某些方面更好的话,我愿意用不同的方式来做。再次感谢!setTimeout只在确定的时间段后运行函数一次。setInterval存在延迟问题。您将在这里看到的最大差异是每次迭代之间执行的代码量。那么在这种情况下你怎么能使用setTimeout呢?不!谢谢你能给我举一个使用setTimeout的例子吗?告诉我你为什么喜欢它。如果在某些方面更好的话,我愿意用不同的方式来做。再次感谢!setTimeout只在确定的时间段后运行函数一次。setInterval存在延迟问题。您将在这里看到的最大差异是每次迭代之间执行的代码量。那么在这种情况下如何使用setTimeout呢?
function animateColor(callback){
    // do stuff
    callback(); // to advance to the next interval
}
function completeFunction(callback){
    // do stuff
    callback(); // to advance to conclude the series of Frames
}
setIntervalX(function () {
    animateColor();
    //or something
}, 3000, 4, function(){
    alert("all done!");
});

function setIntervalX(callback, delay, repetitions, complete) {
    var x = 0;
    var intervalID = window.setInterval(function () {

        callback();

        if (++x === repetitions) {
            window.clearInterval(intervalID);
            complete();
        }
    }, delay);
}