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/
Fiddle: http://jsfiddle.net/9s3zE/1/
Use
setIntervalfor 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 awhileloop for this case, because the browser will freeze when the condition is true.