admin管理员组文章数量:1023044
Just like continue
is used to break current iteration and proceed with the next, how can I break current iteration within setInterval()
in JavaScript and proceed with the next interval without waiting?
var intervalID = window.setInterval( function() {
if(conditionIsTrue) {
// Break this iteration and proceed with the next
// without waiting for 3 seconds.
}
}, 3000 );
Just like continue
is used to break current iteration and proceed with the next, how can I break current iteration within setInterval()
in JavaScript and proceed with the next interval without waiting?
var intervalID = window.setInterval( function() {
if(conditionIsTrue) {
// Break this iteration and proceed with the next
// without waiting for 3 seconds.
}
}, 3000 );
Share
Improve this question
edited May 15, 2021 at 15:40
Michael Rovinsky
7,2507 gold badges17 silver badges32 bronze badges
asked Dec 1, 2012 at 5:59
Aniket SuryavanshiAniket Suryavanshi
1,5843 gold badges14 silver badges24 bronze badges
2
- where is your iteration. Is there any loop? – polin Commented Dec 1, 2012 at 6:04
- 2 Can you give us a little better idea of what you're trying to do. At the moment it sounds like you're trying to blow up your web browser. – Jamund Ferguson Commented Dec 1, 2012 at 6:18
2 Answers
Reset to default 3You could "simply" (or not so simply) clear the interval, and re-create it:
// run the interval function immediately, then start the interval
var restartInterval = function() {
intervalFunction();
intervalID = setInterval(intervalFunction, 3000 );
};
// the function to run each interval
var intervalFunction = function() {
if(conditionIsTrue) {
// Break this iteration and proceed with the next
// without waiting for 3 seconds.
clearInterval(intervalID);
restartInterval();
}
};
// kick-off
var intervalID = window.setInterval(intervalFunction, 3000 );
Here's a demo/test Fiddle.
Just tested this, and it acts as a continue statement in a loop. For fellow coders finding this now, just use return
within the setInterval.
var myRepeater = setInterval( function() {
if(conditionIsTrue) {
return;
}
}, 1000 );
EDIT: To ply with the immediate execution after breaking the current execution of the loop, one could instead do something like so (in theory. And beware of recursion issues if conditionIsTrue
stays true):
function myFunction() {
if(conditionIsTrue) {
myFunction();
return;
}
// Interval function code here...
}
var myRepeater = setInterval( myFunction, 1000 );
Just like continue
is used to break current iteration and proceed with the next, how can I break current iteration within setInterval()
in JavaScript and proceed with the next interval without waiting?
var intervalID = window.setInterval( function() {
if(conditionIsTrue) {
// Break this iteration and proceed with the next
// without waiting for 3 seconds.
}
}, 3000 );
Just like continue
is used to break current iteration and proceed with the next, how can I break current iteration within setInterval()
in JavaScript and proceed with the next interval without waiting?
var intervalID = window.setInterval( function() {
if(conditionIsTrue) {
// Break this iteration and proceed with the next
// without waiting for 3 seconds.
}
}, 3000 );
Share
Improve this question
edited May 15, 2021 at 15:40
Michael Rovinsky
7,2507 gold badges17 silver badges32 bronze badges
asked Dec 1, 2012 at 5:59
Aniket SuryavanshiAniket Suryavanshi
1,5843 gold badges14 silver badges24 bronze badges
2
- where is your iteration. Is there any loop? – polin Commented Dec 1, 2012 at 6:04
- 2 Can you give us a little better idea of what you're trying to do. At the moment it sounds like you're trying to blow up your web browser. – Jamund Ferguson Commented Dec 1, 2012 at 6:18
2 Answers
Reset to default 3You could "simply" (or not so simply) clear the interval, and re-create it:
// run the interval function immediately, then start the interval
var restartInterval = function() {
intervalFunction();
intervalID = setInterval(intervalFunction, 3000 );
};
// the function to run each interval
var intervalFunction = function() {
if(conditionIsTrue) {
// Break this iteration and proceed with the next
// without waiting for 3 seconds.
clearInterval(intervalID);
restartInterval();
}
};
// kick-off
var intervalID = window.setInterval(intervalFunction, 3000 );
Here's a demo/test Fiddle.
Just tested this, and it acts as a continue statement in a loop. For fellow coders finding this now, just use return
within the setInterval.
var myRepeater = setInterval( function() {
if(conditionIsTrue) {
return;
}
}, 1000 );
EDIT: To ply with the immediate execution after breaking the current execution of the loop, one could instead do something like so (in theory. And beware of recursion issues if conditionIsTrue
stays true):
function myFunction() {
if(conditionIsTrue) {
myFunction();
return;
}
// Interval function code here...
}
var myRepeater = setInterval( myFunction, 1000 );
本文标签:
版权声明:本文标题:javascript - How to break current iteration and continue with next iteration within setInterval()? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745524535a2154465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论