I am trying to do so my countdown will only count down whenever the window is active, and then stop whenever the window is not active anymore.
I'm using this plugin: https://github.com/mathiasbynens/jquery-visibility/blob/master/jquery-visibility.js
$(function() {
$(document).on({
'show.visibility': function() {
setTimeout("runCounter()",1000);
console.log('The page gained visibility; the <code>show</code> event was triggered.');
},
'hide.visibility': function() {
time = time;
console.log('The page lost visibility; the <code>hide</code> event was triggered.');
}
});
});
function runCounter() {
if(time==0){
$('#counter').html('<button class="butn blue" onclick="<?php echo $creditButtonName; ?>();">Click here to get credited!</button>');
}
else {
$('#counter').html('You must view this advertisement for ' + time + ' more seconds!');
startBar();
time=time-1;
//setTimeout("runCounter()",1000);
}
}
How is it possible to do so the jquery visibility API will run not just once, but each second when the page is active - and then trigger another function when the page isn't active?
At this current state, this: setTimeout("runCounter()",1000);
will only run whenever I go to another window/tab and then go back to the first window. It will not run each second when the window is active.