Timer In JavaScript

 Why Use Java Script Timer?

In my recent application i have done one task that After 15 sec page redirect automatically and that page display the remaining time.To done that i have use the java script timer.

Step To Achive Timer In Java Script

1). Copy belowed script after Head Tag In your ASPX file

   1: <script language="javascript" type="text/javascript">
   2:     
   3:         var mins,secs,TimerRunning,TimerID;
   4:         TimerRunning=false;
   5:         
   6:         function Init() //call the Init function when u need to start the timer
   7:         {
   8:             mins=0; // set Minite For Redirect
   9:             secs=10; // Set Second For Redirect
  10:             StopTimer();
  11:             StartTimer();
  12:         }
  13:         
  14:         function StopTimer()
  15:         {
  16:             if(TimerRunning)
  17:             clearTimeout(TimerID);
  18:             TimerRunning=false;
  19:         }
  20:         
  21:         function StartTimer()
  22:         {
  23:             TimerRunning=true;
  24:             window.status="Time Remaining "+Pad(mins)+":"+Pad(secs);
  25:             document.getElementById("Label1").innerHTML = "Time Remaining " + Pad(mins) + ":" + Pad(secs);
  26:             TimerID=self.setTimeout("StartTimer()",1000);
  27:  
  28:             Check();
  29:     
  30:             if(mins==0 && secs==0)
  31:             {
  32:                 StopTimer();
  33:                 self.location = "GoogleMap.htm";  // Redirect URL 
  34:             }
  35:     
  36:             if(secs==0)
  37:             {
  38:                 mins--;
  39:                 secs=60;
  40:             }
  41:             secs--;
  42:         }
  43:  
  44:         function Check()
  45:         {
  46:             //if(mins==1 && secs==0)
  47:                //alert("You have only five minutes remaining");
  48:             //else if(mins==0 && secs==0)
  49:             //{
  50:                //alert("Your alloted time is over.");
  51:             //}
  52:         }
  53:  
  54:     function Pad(number) //pads the mins/secs with a 0 if its less than 10
  55:     {
  56:         if(number<10)
  57:             number=0+""+number;
  58:         return number;
  59:     }
  60:     
  61:     </script>

2). Paste this line between form tag of your ASPX page.

   1: <div>
   2:        <asp:Label ID="Label1" Width="205px" runat="server"></asp:Label>
   3: </div>

3). After Finish all steps that mention above at last put this code on body tag.

   1: onload="Init();"
Advertisements

2 thoughts on “Timer In JavaScript

  1. Tessia

    Just say there is an asp log in page with that javascript timer on the page, if the user logs in and get access to the browser and go to random sites such as yahoo, google, etc… Will it redirect automatically after the timer is up or is it useless once they leave the created asp pages?

  2. Tessia

    Love the code but it seems impossible for the javascript to call it back to my created .aspx page once they have left my pages and went to random sites such as google.com even after the timer has started.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s