始终进行 while 循环测试
我正在做一个使用 while 循环的简单测试。
这是代码:
var ishovered = 0;
$('a.hovertest').hover(function(){
ishovered = 1;
},function(){
ishovered = 0;
});
while(ishovered == 1)
{
//execute function
}
如何使 while 循环一直在测试并且函数在他悬停时立即执行? (是的,我知道我可以将函数放在悬停部分,我问这个是有原因的)
不过它并不一定是一个循环,我只需要一个函数在 ishovered 变为 1 时立即执行。有什么办法可以监控吗?
I'm working on a simple test that uses a while loop.
Here is the code:
var ishovered = 0;
$('a.hovertest').hover(function(){
ishovered = 1;
},function(){
ishovered = 0;
});
while(ishovered == 1)
{
//execute function
}
How do I make it so that while loop is testing all the time and the function executes as soon as he hovers? (Yes, I know I can put the function in the hover section, I'm asking this for a reason)
It doesn't really have to be a loop though, I just need a function to execute as soon as ishovered becomes 1. Is there any way to monitor that?
Here is the example: http://jsfiddle.net/9s3zE/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
小提琴:http://jsfiddle.net/9s3zE/1/
使用
setInterval
用于此目的。顺便说一句,您正在描述“轮询者”的行为。轮询器检查条件是否计算为 true。如果为真,则激活一个功能。如果为 false,则发生默认行为(可能什么也不发生)。对于这种情况,使用while
循环并不明智,因为当条件为真时,浏览器将冻结。Fiddle: http://jsfiddle.net/9s3zE/1/
Use
setInterval
for this purpose. You're describing the behaviour of a "poller", by the way. A poller checks whether a condition evaluates to true. If true, a function is activated. If false, the default behaviour (possible nothing) occurs. It's not wise to use awhile
loop for this case, because the browser will freeze when the condition is true.