-3

I am trying to make a sequence of numbers that starts with number 10 and grows like 10...11...12...13 etc every two seconds. But lets say that I want it to stop when it reaches 100, how do i do it. So far I have this.Any ideas?

function conteo(num){
setInterval(function(){document.write(num++ + "..."); }, 2000);

}conteo(10)

2 Answers2

0

You can clear the interval:

function conteo(num){
    var interval = setInterval(function() {
        if(num == 100) {
            clearInterval(interval);
        }
        document.write(num++ + "..."); 
    }, 2000);
}
conteo(10)

This will check if num is equal to 100, then clear interval if true, but keep going.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
0

Save the setInterval reference call into a variable:

var conteoInterval;

function conteo(num){
  conteoInterval = setInterval(function(){document.write(num++ + "..."); },  2000);
}

And to stop the interval, just clear its reference, doing this:

clearInterval(conteoInterval);
daymannovaes
  • 1,416
  • 12
  • 23