javascript/jQuery setInterval/clearInterval [英] javascript/jQuery setInterval/clearInterval
问题描述
我正在使用setInterval来检查p(html段落)是否具有特定的文本值.如果有它,我想清除间隔继续代码流.我在jQuery插件中使用了此功能,因此,如果该段落具有tat文本值,我想清除时间间隔,然后继续使用回调函数. 所以我尝试了这样的事情:
i'm using setInterval to check if a p(html paragraph) has a certain text value. if it has it i want to clear interval an continue code flow. i'm using this in a jQuery plugin so if the paragraph has tat text value i want to clear interval and then continue with a callback function. so i tried something like this:
var checkTextValue = setInterval(function(){
var textVal = $('p').text();
if(textVal == 'expectedValue'){
clearInterval(checkTextValue);
callback();
}
},10);
和回调函数,这是一个简单的警报. 我的问题是警报被无休止地调用.如何编写我的代码以正确执行操作?谢谢.
and the callback function it's a simple alert. My problem is that the alert is called endlessly. How can i write my code to do it right? thanks.
推荐答案
使用setTimeout代替setInterval.
Use setTimeout instead of setInterval.
类似的东西:
var checkTextValue = setTimeout(function() {
var textVal = $('p').text();
if (textVal == 'expectedValue'){
callback();
} else {
setTimeout(arguments.callee, 10);
}
},10);
这篇关于javascript/jQuery setInterval/clearInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!