admin管理员组文章数量:1136498
I've created a loop of "changing words" with jQuery by using the code in this answer: jQuery: Find word and change every few seconds
How do I stop it after some time? Say after either 60 seconds or after it has gone through the loop?
(function() {
// List your words here:
var words = [
'Lärare',
'Rektor',
'Studievägledare',
'Lärare',
'Skolsyster',
'Lärare',
'Skolpsykolog',
'Administratör'
],
i = 0;
setInterval(function() {
$('#dennaText').fadeOut(function() {
$(this).html(words[i = (i + 1) % words.length]).fadeIn();
});
// 2 seconds
}, 2000);
})();
I've created a loop of "changing words" with jQuery by using the code in this answer: jQuery: Find word and change every few seconds
How do I stop it after some time? Say after either 60 seconds or after it has gone through the loop?
(function() {
// List your words here:
var words = [
'Lärare',
'Rektor',
'Studievägledare',
'Lärare',
'Skolsyster',
'Lärare',
'Skolpsykolog',
'Administratör'
],
i = 0;
setInterval(function() {
$('#dennaText').fadeOut(function() {
$(this).html(words[i = (i + 1) % words.length]).fadeIn();
});
// 2 seconds
}, 2000);
})();
Share
Improve this question
edited Aug 27, 2020 at 8:20
Ivar
6,78912 gold badges56 silver badges67 bronze badges
asked Feb 3, 2012 at 22:15
AlissoAlisso
1,9021 gold badge17 silver badges33 bronze badges
6 Answers
Reset to default 174To stop it after running a set number of times, just add a counter to the interval, then when it reached that number clear it.
e.g.
var timesRun = 0;
var interval = setInterval(function(){
timesRun += 1;
if(timesRun === 60){
clearInterval(interval);
}
//do whatever here..
}, 2000);
If you want to stop it after a set time has passed (e.g. 1 minute) you can do:
var startTime = new Date().getTime();
var interval = setInterval(function(){
if(new Date().getTime() - startTime > 60000){
clearInterval(interval);
return;
}
//do whatever here..
}, 2000);
Use clearInterval
to clear the interval. You need to pass the interval id which you get from setInterval
method.
E.g.
var intervalId = setInterval(function(){
....
}, 1000);
To clear the above interval use
clearInterval(intervalId);
You can change your code as below.
(function(){
// List your words here:
var words = [
'Lärare',
'Rektor',
'Studievägledare',
'Lärare',
'Skolsyster',
'Lärare',
'Skolpsykolog',
'Administratör'
], i = 0;
var intervalId = setInterval(function(){
$('#dennaText').fadeOut(function(){
$(this).html(words[i=(i+1)%words.length]).fadeIn();
if(i == words.length){//All the words are displayed clear interval
clearInterval(intervalId);
}
});
// 2 seconds
}, 2000);
})();
The simplest solution is
var intervalId = setInterval(function() {
$('#dennaText').fadeOut(function() {
$(this).html(words[i = (i + 1) % words.length]).fadeIn();
});
}, 2000); // run every 2 seconds
setTimeout(function(){
clearInterval(intervalId);
},10000) // stop it after 10seconds
You should consider using a recursive setTimeout()
instead of setInterval()
to avoid a race condition.
var fadecount = 1;
(function interval(){
$('#dennaText').fadeOut(function(){
$(this).html(words[i=(i+1)%words.length]).fadeIn('fast',function(){
if (fadecount < 30){
fadecount += 1;
setTimeout(interval, 2000);
}
});
});
}());
You can use setTimeout
instead, which is better:
(function foo(){ // wrap everything in a self-invoking function, not to expose "times"
times = 20; // how many times to run
(function run(){
// do your stuff, like print the iteration
document.body.innerHTML = times;
if( --times ) // 200 * 20 = 4 seconds
setTimeout(run, 100);
})();
})();
I'm working with VueJs and wanted to remove a div after few seconds; here's what I did, I hope that could help someone ;).
export default {
name: "Home",
components: {},
data() {
return {
removeAlert: false,
};
},
methods: {
removeAlertF() {
let wait = window.setInterval(() => {
this.removeAlert = true;
console.log("done");
if(true){
window.clearInterval(wait);
}
}, 3000);
},
},
mounted() {
this.loadData();
this.removeAlertF();
},
};
<style>
.remove-alert {
display: none;
}
</style>
<div
:class="removeAlert ? 'remove-alert' : ''"
role="alert"
>
Data loaded successfully
</div>
本文标签: javascriptHow to make a setInterval stop after some time or after a number of actionsStack Overflow
版权声明:本文标题:javascript - How to make a setInterval stop after some time or after a number of actions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736925733a1956613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论