1

I am running a page with a timer that I'm using to run in a iframe of a page so that I know that someone was on there actually with window in focus paying attention to screen and if leave have the timer pause until back viewing the window or in focus.

I have 4 timer files: timer.css timer.js timer.php & timerb.js

I believe I would execute it in the following file and code, but that is where I am stuck as don't know what or where to add it for sure and no luck yet and why here asking so please take a look and let me know if you can help.

File: timer.js

function adTimer() {
  timer++;
  if(timer == fulltimer) {
    var show="Click "+key;
    $("#buttons").fadeIn();
    $("#timer").html(show);
  }
  else {
    setTimeout(adTimer, 1000);
  }
  $("#bar").width((timer/fulltimer)*200);
}
RikyTres
  • 676
  • 9
  • 31
silas82
  • 11
  • 2

2 Answers2

1

This could be enough for you:

$(window).blur(function(){
  //your code for inactive
});
$(window).focus(function(){
  //your code for active
});

or non jQuery solution (https://stackoverflow.com/a/1760283)

window.onblur = function () { 
  //your code for inactive
}; 
window.onfocus = function () { 
  //your code for active
}; 

if not try Page Visibility API (answer already here https://stackoverflow.com/a/1060034)

Community
  • 1
  • 1
stove
  • 556
  • 6
  • 24
0

Just wondering why all that implementation if JQuery already provide a timeout. did you try to use set Timeout. http://www.sitepoint.com/settimeout-example/

paul
  • 12,873
  • 23
  • 91
  • 153
  • Cause after timer does complete I am executing a captcha or question about the viewing then rewarding points if answered correctly. and no I have not tried as tried few things seen on here but never any help as I am a noob to this type of programming. – silas82 Feb 20 '15 at 08:08