setInterval和clearInterval,如何只运行一次? [英] setInterval and clearInterval, How to run only 1 time?
本文介绍了setInterval和clearInterval,如何只运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
timerA = setInterval(function()
{
// codes ..
clearInterval(timerA);
},2000);
我只想调用setInterval中的函数1次。我怎么用setInterval和clearInterval做到这一点?
还有另外一种方法可以做到吗?
解决方案
使用如果您只希望它运行一次,请使用setTimeout方法。
示例:
setTimeout(function(){
// 5秒后做某事
},5000);
I only want to run the function 1 time.
timerA = setInterval(function()
{
//codes..
clearInterval(timerA);
}, 2000);
I want to call the function inside setInterval only 1 time. How can I do it with setInterval and clearInterval?
Or is there another technique to do it?
解决方案
Use the setTimeout method if you only want it to run once.
Example:
setTimeout(function() {
// Do something after 5 seconds
}, 5000);
这篇关于setInterval和clearInterval,如何只运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文