Sebastian Schneider
Sebastian Schneider

Reputation: 5552

setInterval not working with document.ready

I'm completely confused as I'm not a Javascript beginner...

$(document).ready(function(){
  var loadtime = 2000;
  var loadtimer = setInterval(function(){
    loadtime = loadtime - 100;
    console.log(loadtime);
  }, 100);
});

This is not working for me. The console output is: 1900

I want the script to output every 100 milliseconds the current loadtime.

Where is my fault?

Thanks in advance.

Upvotes: 0

Views: 566

Answers (1)

Daniel
Daniel

Reputation: 18692

It does work - check this demo on JSFiddle.

  var loadtime = 2000;
  var loadtimer = setInterval(function(){
    loadtime = loadtime - 100;
    console.log(loadtime);
  }, 100);

edit

The reason it doesn't work is that you call:

clearInterval(loadtimer);

Which stops your setInterval function about after first run.

Upvotes: 2

Related Questions