I have a counter that counts down when windows is on focus and stops counting when window is on blur.
My code works perfectly with Firefox and IE because with FF or IE, focus() event is triggered upon load of the document.
But in Safari/Chrome it’s not.
Does anyone know how I can trigger the countDown() function upon loading with Safari/Chrome?
Thanks a lot
my code has this structure:
$(document).ready(function(){
$(window).focus(function(){
countDown();
});
$(window).blur(function(){
stopCount();
});
});
How about you call
countDownfrom ready aswell? You might have to check if the window still has focus first though. And you should make surecountDownwasn’t called already – or make countDown “resistant” against additional calls.Or another way, that should make the code a bit cleaner is to simply call
$(document).focus()from$(document).ready.More context: