Problem with stopping time when opened tab is changed or not..........

I am using setTimeout function to run time in my javascript application ....is there any method that i can stop application when the tab where timer is running changes..........?????
savsoftAsked:
Who is Participating?
 
Gurvinder Pal SinghConnect With a Mentor Commented:
window.onblur = function() {
   //stop the timer
};

window.onfocus = function() {
   //start the timer from the last value
};
0
 
Gurvinder Pal SinghCommented:
if you want don't want the user to trigger any event on the screen when the timer is ticking, then you need to overlay a transparent div on top of your screen.
0
 
savsoftAuthor Commented:
i am using setTimeout fintion to run timer in one page i want that if any one open or minimize that page/tab then time will pause.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
savsoftAuthor Commented:
But I also want tomaintain the time where tab is changed and time coming back too
0
 
Gurvinder Pal SinghCommented:
you can save the timer value at the blur event and start the timer again from saved value on focus, right?
0
 
leakim971PluritechnicianCommented:
update your setTimeout :

<script language="javascript" type="text/javascript">

var pause = false; // default value

setTimeout(function() {
         if( !pause ) {
             // your current code here
             // I suppose you've an other setTimeout here too
         }
}, 1000); // 1000 is your current delay

to stop it, just set pause to true :

pause = true;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.