Sunday, February 10, 2013

Simple Way to create Timer using Javascript

Timer_Javascript
In this article i would like to explain how to create a simple timer control using javascript.In my previous article's i already explained about Get Title and meta Tags using Asp.net and c#, and How to track user information while and Login and Logout using Asp.net and c#. Here I am going to share the trick for creation of Javascript Timer control.
Following is the main logic to display dynamic timer using javascript.

Main Logic in Javascript:
facebook_login_button

   function display() {
    if (seconds > 60) {
            minutessplit = seconds / 60;          
            minutes = [(minutessplit > 0) ? Math.floor(minutessplit) : Math.ceil(minutessplit)];
            seconds = (minutes * 60) - seconds;
        } 
    gettime();
}
function gettime() {
    if (milisec <= 0) {
        milisec = 9;
        seconds -= 1;
    }
    if (seconds <= -1) {
        seconds = 60;
        milisec = 0;
        minutes -= 1;

    }
    if (minutes <= -1) {
        seconds = 0;
        minutes += 1;
    }
    else

        milisec -= 1
    document.getElementById('d2').value = minutes + ":" + seconds + " : " + milisec;
    if (minutes == 0 && seconds == 0 && milisec == 0) {
        alert("Time Out");
        
    }
    else {
        setTimeout("gettime()", 100)
    }
}
Design part in Html

  • Timer : 10-Secs
  • Timer : 30-Secs
  • Timer : 1-Min
  • Timer : 2-Mins
  • Timer : 3-Mins
That's it..Please let me know if you have any issues.

Labels: , , ,