3

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.

3
  • There are no variants of time. Why are you doing this? You can just have one timer, set a beginning value and check how it's going. Commented Feb 23, 2012 at 6:30
  • Of course you can modify the speed while the timer is running, it just needs to be a avariable. Commented Feb 23, 2012 at 6:42
  • @ ingyhere I am doing a game(quiz) in which for each question I want decrement timer from 500 so for each question i called function CreateTimer() but as i call CreateTimer() function twice,thrise and so on speed is increasing .I am not understanding why this happens why timer speed is not same at all time. Commented Feb 23, 2012 at 6:42

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ok Thank you problem solved I am not using clearInterval() function.
3

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>

Comments

2

Check this demo on jsFiddle.net.

HTML

<div id="divTimer">100</div>
<div id="divDec">1</div>
<div id="divSpeed">100</div>

<input id="start" value=" Start " onclick="javaScript:start();" type="button" />
<input id="stop" value=" Stop " onclick="javaScript:stop();" type="button" /><br/>

<input id="inc" value=" Increase " onclick="javaScript:increase();" type="button" />
<input id="dec" value=" Decrease " type="button" onclick="javaScript:decrease();"/>​

JavaScript

 var handler;

function start() {
    handler = setInterval("decrementValue()", parseInt(document.getElementById('divSpeed').innerHTML, 10));
}

function stop() {
    clearInterval(handler);
}

function decrementValue() {
    document.getElementById('divTimer').innerHTML = parseInt(document.getElementById('divTimer').innerHTML, 10) - parseInt(document.getElementById('divDec').innerHTML, 10);
}

function increase() {
    document.getElementById('divDec').innerHTML = parseInt(document.getElementById('divDec').innerHTML, 10) + 1;
    document.getElementById('divSpeed').innerHTML = parseInt(document.getElementById('divSpeed').innerHTML, 10) + 200;
    stop();
    decrementValue();
    start();
}

function decrease() {
    document.getElementById('divDec').innerHTML = parseInt(document.getElementById('divDec').innerHTML, 10) - 1;
    document.getElementById('divSpeed').innerHTML = parseInt(document.getElementById('divSpeed').innerHTML, 10) - 200;
    stop();
    decrementValue();
    start();
}​

Hope this is what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.