0

the code :

function startTimer() {
  var i = 0;
  var TimeInterval = setInterval(function () {
    $("#Time").html(i++);
  }, 1000);
  setTimeout(function () {
    clearInterval(TimeInterval);
  }, 2000);
}

my problem is that $("#Time").html(i++) is always 0 and not incrementing

7
  • I think the problem is that you're stopping the interval before it has chance to run a 2nd time (1st time will show 0, but increment i for next time) Commented Jul 24, 2013 at 9:45
  • what do you suggest ? Commented Jul 24, 2013 at 9:45
  • What are you actually trying to do, because all of the above can be done by getting rid of both timers and just putting $("#Time").html("1") - obviously I know that's not what you want. Commented Jul 24, 2013 at 9:46
  • @Sora try commenting clearInterval(). Commented Jul 24, 2013 at 9:47
  • 1
    then you should place 120000 instead of 2000 in clearInterval :) because 2000 is 2 seconds, not 2 minutes :) Commented Jul 24, 2013 at 9:53

4 Answers 4

1
<!DOCTYPE html>
<html>
<head>
<script type = 'text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'></script>
<script>
(function startTimer() {//selef executing function
 var i = 0;
 var TimeInterval = setInterval(function () {
    $("#button").html(i++);
   }, 1000);
   })()//end of function
</script>
</head>
<body>
 <p id='button'></p>
 </body> 
</html>

set time out should be removed from your code then it will work.see the above code.it works

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

Comments

0
function startTimer() {
  var i = 0;
  var TimeInterval = setInterval(function () {
    $("#Time").html(++i);
  }, 1000); // This will run every 1000ms == 1s
  setTimeout(function () {
    clearInterval(TimeInterval);
  }, 2000); // This is 2000 ms == 2s. 120000ms is 2min
}

I've only changed i++ to ++i - but the difference is probably what you want.

With i++, i is evaluated first, then incremented - with ++i, i is first incremented, then evaluated.

Comments

0
function startTimer() {
  var i = 0;
  var TimeInterval = setInterval(function () {
    $("#Time").html(i++);
  }, 1000);
  setTimeout(function () {
    clearInterval(TimeInterval);
  }, 120000);
}

this code increments the html each second, until 2 minutes are passed :)

Comments

0

I think this is easiest with just one timeout that reschedules itself:

function countForTwoMins() {
    var count = 1,
        timer = function () {
            $("#Time").html(count++);
        };
    window.setTimeout(function() {
        timer();
        if (count < 120) {
            window.setTimeout(arguments.callee, 1000);
        }
    }, 1000);
}

countForTwoMins();

http://jsfiddle.net/ybyu5/

Of course, if you care about arguments.callee being deprecated:

function countForTwoMins() {
    var count = 1,

    function callback () {
        $("#Time").html(count++);
        if (count < 120) {
            window.setTimeout(callback, 1000);
        }
    }        
    window.setTimeout(callback, 1000);
}

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.