-2

I have two example code.
The two are same purpose, to increase the variable count by 1, and print the value.
But the first example using setInterval, and the second example using setTimeout.

var count = 0;

// First example.
var time = setInterval(function() {
   document.body.innerHTML = count;
   count++;
}, 1000);

// Second example.         
var time = setTimeout(function() {
   document.body.innerHTML = count;
   count++;
}, 1000);

Why in the first example increases the value of variable and prints its value Continuously, but in the second example increases the value of variable by 1 and prints its value once Then stops and does not continue?

I ask this question, because I've seen some examples work continuously using setTimeout also.

Lion King
  • 32,851
  • 25
  • 81
  • 143
  • 5
    Because that's the difference between `setTimeout` and `setInterval`. – elclanrs Dec 24 '13 at 01:34
  • Why somebody voted down my question, what's wrong that I done? – Lion King Dec 24 '13 at 01:38
  • 1
    I didn't vote, but you've not given any reason why these functions fail to meet your expectation. Why would they not behave the way they do? Why would you expect them to behave the same? What would be the point of having two functions providing identical functionality? There seems to be a lack of basic research, and perhaps basic reasoning. – cookie monster Dec 24 '13 at 01:41
  • 1
    possible duplicate of ['setInterval' vs 'setTimeout'](http://stackoverflow.com/questions/2696692/setinterval-vs-settimeout). Being a beginner doesn't excuse one from the task of research. – cookie monster Dec 24 '13 at 01:44
  • Please everybody you don't dealing with anybody ask a question as a professional – Lion King Dec 24 '13 at 01:52
  • 1
    You don't need to be a professional to use Google. – cookie monster Dec 24 '13 at 01:58

3 Answers3

3

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. SetTimeout will be called only once.

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.

gorgi93
  • 2,457
  • 8
  • 30
  • 53
2

Seriously?

Because setTimeout() and setInterval() are not the same function...

The behaviour you have observed is what has been specified for each function.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

If you want setTimeout to "behave" like setInterval you should do something like this

var time = function(){
    document.body.innerHTML = count;
    count++;
    setTimeout(time, 1000);
}

time();
lleon
  • 2,615
  • 1
  • 20
  • 14
  • Yes, that is what I mean, because I've seen some examples work continuously using `setTimeout`. – Lion King Dec 24 '13 at 01:57
  • Thank you, but can you please tell me how this code work step by step (`The implementation priority`) ? – Lion King Dec 24 '13 at 02:06
  • First, a function called time is declared. The function write the variable 'count' in the body, and then the function calls to itself every 1000 ms – lleon Dec 24 '13 at 02:13