0

I have seen the following working! setinterval call.

window.setInterval('checkhash();', 1);

Any idea why this is working?

normaly it is like this:

setInterval(function(){
      do something
},1000);


function checkhash(){ };
hamburger
  • 1,339
  • 4
  • 20
  • 41
  • 1
    What's exactly the question here ? The first one, while bad because it asks the engine to repetitively eval the string, do work (if checkhash is a function). – Denys Séguret Feb 25 '14 at 12:27
  • I do not understand why the first statement is working. If I copy it to my work i will get an error. – hamburger Feb 25 '14 at 12:35

3 Answers3

7

You have several options:

1) hand over a string which is then evaluated (bad - never do this!)

window.setInterval('checkhash()', 4);

This basically runs an evaluation on the string like eval('checkhash()').

Important to know, is that it is evaluated in the global context, so the following will fail:

(function(){
    function test(){alert("foo")};
    function test2(){alert("bar")};

    // will fail
    setTimeout("test()",1000);


    // will work
    setTimeout(test2,1000);

})();

The first timeout will produce an Uncaught ReferenceError: test is not defined, because test() is only known in the context of your anonymous function, but not in the global space:
See example

2) hand over a function reference (the preferred way)

window.setInterval(checkhash, 4);

3) hand over an anonymous function (used, when you need to hand over parameters)

setInterval(function(){
      checkhash(param1,param2);
},4);

Doing the following (common beginners mistake) is possible too:

window.setInterval(checkhash(), 4);

This will call the function immediately and hand over the return value to the timeout to be executed after the specified amount of time.

Important notes:

  • the execution is asynchronous - the javscript engine will continue with the execution of the rest of your js code.
  • The minimal timeout is 4 ms as per spec, so basically 0-3 will give you an interval of at least 4ms (depending on the scheduler of course).
  • You have no guarantee that the timeout will trigger in exactly that amount of time. It might even be completely discarded in some very rare circumstances.

A very good reading is John Resig's article about timers.

MDN setTimeout documentation

Christoph
  • 50,121
  • 21
  • 99
  • 128
0

this should be like,

window.setInterval(checkhash, 1);

remove your quotes, and do like this.

Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
0

This is one of the all the JavaScript eval situations, which is not a good practice, if you use string as the first parameter, it automatically get converted to a anonymous function like this:

Function('checkhash();')

So why not passing the first parameter as a function, which is the other alternative and more reasonable one.

window.setInterval(function(){
    checkhash();
}, 1);

or simply:

window.setInterval(checkhash, 1);

the only problem this way is sometimes you need to use JavaScript closures to keep you your variable values stable.

Community
  • 1
  • 1
Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35