Timer problem when window minimized

Timer problem when window minimized

Dear friends,

I am a .NET WEB APPLICATION DEVELOPER... 

I just started using JQUERY and liked it so much. I developed an asp.net page and used some jquery code on it.

Shortly, I put a timer bar with 900px width. I use an animate function. Function adds 5px every second and if width of the bar exceeds 895px, timer stops and width gets 0px.

You will find the code below. 

The problem is if I minimize or open another window while timer works, width of the bar exceeds the 895px but timer does not stop.

I could not find the solution. I need your support.

Thanks in advance for your supports.

Regards,

  1. $(document).ready(function() {
  2. var myTimer = null;
  3. var myParamKrono = 1;
  4. }

  5.  if (myParamKrono == 1) {
  6.                         myTimer = setInterval(timerFonks, 1000);
  7.  }

  8. //TIMER FUNCTION
  9.         function timerFonks() {
  10.             var myWidth = $("#Bar").width();
  11.             //alert(myWidth);
  12.             $("#Bar").animate({ width: '+=5px' }, "slow", function() {
  13.                 
  14.                 if (myWidth < 300) {
  15.                     $("#Bar").css("background-color", "blue");
  16.                 } else if (myWidth < 600) {
  17.                     $("#Bar").css("background-color", "yellow");
  18.                 } else if (myWidth < 895) {
  19.                     $("#Bar").css("background-color", "red");
  20.                 } else {
  21.                     clearInterval(myTimer);
  22.                     $("#Bar").animate({ width: '0px' });
  23.                 };
  24.             });
  25.         };