2013-08-18 35 views
0

我有一个JavaScript代码可以在页面上生成一些元素。但由于这些新生成的元素的数据来自外部源,因此可能需要同时为这些元素加载。 我想做的事情是,我想在这些元素加载后做一些事情。所以我决定我应该检查元素是否每秒都出现,然后执行其他的Coeds。如何在事件处理程序中设置计时器并在条件下停止计时器

这就是我想做的事:

//start a timer like event handler which occurs every second 
//in every second{ 
    if ($(".elementthatmustappear").is(":visible")) 
    { 
     //stop the timer 
     //execute other codes 
    } 
    else 
    { 
     //don't stop the timer.continue checking 
    } 
} 

有没有这样做的一种方式?

+1

可能的重复:[?如何停止的setTimeout循环(HTTP://计算器.COM /问题/ 8443151 /如何到停止-A-的setTimeout环) – Itay

回答

0

The thing I want to do is that I want to do some things AFTER these elements are loaded.

在这种情况下,它听起来就像你应该通过一个回调函数加载内容,应在加载后执行。

如果你决定要跟着你已经映射出的逻辑,这应该工作:

var timer = setInterval(function() { 
    if ($(".elementthatmustappear").is(":visible")) { 
     clearInterval(timer); 
     doSomethingElse() 
    } 
}, 1000); 
相关问题