Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timer using javascript

I want implement timer using java script.I want to decrement timer with variation of interval.
Example.Suppose my timer starts at 500 .
I want decrement timer depending on the level such as
1. 1st level timer should decrement by 1 also decrement speed should be slow.
2.2nd level timer should decrement by 2 and decrement speed should be medium
3.3rd level timer should decrement by 3 and decrement speed should be fast

I can create timer using following code:

<script type="text/javascript">
var Timer;
var TotalSeconds;
function CreateTimer(TimerID, Time) 
{
    TotalSeconds=Time;
    Timer = document.getElementById(TimerID);
    TotalSeconds = Time;
    UpdateTimer();
    setTimeout("Tick()", 1000);
}

function Tick() {
    TotalSeconds -= 10;
    if (TotalSeconds>=1)
    {
        UpdateTimer();
        setTimeout("Tick()", 1000);
    }
    else
    {   
        alert(" Time out ");
        TotalSeconds=1;
        Timer.innerHTML = 1;
    }
}

But i call this CreateTimer() function many times so its speed is not controlling because i call it many times.

like image 925
Coder Guru Avatar asked Dec 05 '25 02:12

Coder Guru


2 Answers

Couple of points:

  1. You've used all global variables, it's better to keep them private so other functions don't mess with them
  2. Function names starting with a captial letter are, by convention, reserved for constructors
  3. The function assigned to setTimeout doesn't have any public variables or functions to modify the speed while it's running so you can only use global variables to control the speed. That's OK if you don't care about others messing with them, but better to keep them private
  4. The code for UpdateTimer hasn't been included
  5. Instead of passing a string to setTimeout, pass a function reference: setTimeout(Tick, 1000);

Anyhow, if you want a simple timer that you can change the speed of:

<script>

var timer = (function() {
    var basePeriod = 1000;
    var currentSpeed = 1;
    var timerElement;
    var timeoutRef;
    var count = 0;

    return {
      start : function(speed, id) {
        if (speed >= 0) {
          currentSpeed = speed;
        }
        if (id) {
          timerElement = document.getElementById(id);
        }
        timer.run();
      },

      run: function() {
        if (timeoutRef) clearInterval(timeoutRef);
        if (timerElement) {
          timerElement.innerHTML = count;
        }
        if (currentSpeed) {
          timeoutRef = setTimeout(timer.run, basePeriod/currentSpeed);
        }
        ++count;
      },

      setSpeed: function(speed) {
        currentSpeed = +speed;
        timer.run();
      }
    }

}());

window.onload = function(){timer.start(10, 'timer');};

</script>

<div id="timer"></div>
<input id="i0">
<button onclick="
  timer.setSpeed(document.getElementById('i0').value);
">Set new speed</button>

It keeps all its variables in closures so only the function can modify them. You can pause it by setting a speed of zero.

like image 67
RobG Avatar answered Dec 07 '25 19:12

RobG


Hope, this could be helpful:

<html>
<head>
<script type="text/javascript">
function showAlert()
{
var t=setTimeout("alertMsg()",5000);
}
function alertMsg()
{
alert("Time up!!!");
}
</script>
</head>

<body>
<form>
<input type="button" value="Start" onClick="timeMsg()" />
</form>
</body>

</html>
like image 34
Virtual Avatar answered Dec 07 '25 19:12

Virtual



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!