Monday, 9 September 2013

Using jQuery to recall a function window onblur+ onfocus

Using jQuery to recall a function window onblur+ onfocus

hI got a problem to use jQuery to recall afunction if window is on focus.
And when window is not on focus (onblur) so pause that function until
window is on focus again.
Here is my code:
function functiondosomething (user_id) {
var myInterval;
var time_delay = 1000;
$(window).focus(function () {
setTimeout(function() { functiondosomething (user_id); }, time_delay);
}).blur(function () {
clearTimeout(myInterval); // Clearing interval on window blur
});
setTimeout(function() { functiondosomething (user_id); }, time_delay);//
## problem here
}
My problem is :
When I remove that line (which I marked problem here above.) the function
will not work at first time until I click out of window to make it onblur
and come back on focus again, so it starting to work.
If I let that line (which I marked problem here above.) be there, the
function could not pause, even I click out of window to make it be onblur.
When I click onfocus it start working and stop. I have to click out of
window and focus the window again again and again. Something like it need
to be activate by clicking out of window and clicking back to window
again.
What should I do ?

No comments:

Post a Comment