Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInterval inside a function produces an error: variable is not defined

Tags:

javascript

I do not understand what is wrong. I have three codes:
First:

<script language="JavaScript" type="text/javascript">
   var count = 0;
   alert(count);
   var timer = setInterval("count = count + 1; alert(count);",10000);
</script>



Second:

<script language="JavaScript" type="text/javascript">
  function countdown()
  {
   var count = 0;
   alert(count);
   var timer = setInterval("count = count + 1; alert(count);",10000);
  }
  countdown();
</script>



Third:

<script language="JavaScript" type="text/javascript">
   var count = 0;
  function countdown()
  {
   alert(count);
   var timer = setInterval("count = count + 1; alert(count);",10000);
  }
  countdown();
</script>



The first code works fine, the second produces an error in the "setInterval" line: "count is not defined", and the third code works fine again. The scope of the "count" variable should be global for the setInterval function in the second code. Why is it not? I am using Mozilla Firefox. Thanks.

like image 319
GreenBear Avatar asked Nov 10 '12 21:11

GreenBear


People also ask

Why is setInterval not accurate?

why is setInterval inaccurate? As we know, setTimeout means to run the script after the minimum threshold (MS unit), and setInterval means to continuously execute a specified script with the minimum threshold value period. Note that I use the term minimum threshold here because it is not always accurate.

How does the setInterval () function work in?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .

Does the setInterval () function work in JavaScript?

The setInterval() method in JavaScript is used to repeat a specified function at every given time-interval. It evaluates an expression or calls a function at given intervals. This method continues the calling of function until the window is closed or the clearInterval() method is called.

How would you stop a setInterval () once it has been set?

Answer: Use the clearInterval() Method The setInterval() method returns an interval ID which uniquely identifies the interval. You can pass this interval ID to the global clearInterval() method to cancel or stop setInterval() call.


1 Answers

For a great number of reasons, one of which you just ran into, never ever pass a string to setTimeout or setInterval. Ever. I mean it. There is never a good reason.

Pass a function instead. The ability to pass function objects around is one JS best features.

var count = 0;
alert(count);

var timer = setInterval(function(){
  count = count + 1;
  alert(count);
}, 10000);

The problem you are facing is that code as a string in this manner won't respect scope. It will execute in the global scope, which is a place your variable doesn't exist in your 2nd and 3rd snippets. And the first snippet works because count is indeed a global variable.

Other problems with this stem from the fact this is basically eval which comes with its own headaches and is best to avoid entirely. Eval is Evil after all.

like image 133
Alex Wayne Avatar answered Sep 23 '22 15:09

Alex Wayne