Execute a piece of js code every given seconds

Hi,

How can I execute a piece of js code every given seconds?
I have included a js file in a html and I have written:


window.onload = init;

function init() {
   setTimeout("function_to_call()", 1000);
}

but without success. The function_to_call has been executed only one time and then it stops.

Can you help me please?

thanks.

setTimeout only occurs once, you need to use setInterval

function init() {
   setInterval(function_to_call, 1000);
}

The first parameter to setTimeout and setInterval is a function. Passing it a string means it has to first eval() the string into a function before it can run it.

You can use setTimeout by including another line in function_to_call re-calling init().

Like this:

function init() {
setInterval(“function_to_call()”, 1000);
}
// -----
function function_to_call() {
// do whatever
init();
}

That’s a very bad idea, and not only because of the implied eval that occurs on the strings.

Seconds:
1: execute init()
2. execute init(), execute function_to_call
3. execute init(), execute function_to_call, execute init()
4. execute init(), execute function_to_call, execute init(), execute function_to_call
etc…

Were you meaning to use setTimeout instead?

Another option

window.onload = init;

function init() {
   //do other stuff here
   function_to_call();
}

function function_to_call() {
    //do something
    setTimeout(function_to_call, 2000);
}

function init() {
setInterval(“function_to_call()”, 1000);
}

Sorry! that should of course have been

function init() {
setTimeout(“function_to_call()”, 1000);
}

NO. NO. NO. It needs to be:

function init() {
setTimeout(function_to_call, 1000);
}

The first parameter to setTimeout needs to be a function. If you pass it a string then the browser has to convert the string into a function before it can pass the function to the setTimeout.

Repeat to yourself ten thousand times so you don’t forget: “setTimeout takes a function and not a string - never ever use a string to represent a function”.

I beg to differ here. The correct syntax is either
setTimeout(“script Expression”,msecs[, language]) or,
setTimeout(functionReference,msecs[, arg1, …argN])

Ref: Dynamic HTML, D.Goodman. page 1010.

So we are both right :slight_smile:

There are though significant reasons why one is preferred over the other.

I agree with felgall and pmw57.

Although you can technically use a string, passing a reference to the function is a much better way of doing it as they explained earlier.

I am curious. What are these significant and preferred reasons?

One of the major ones is that a whole new instance of the javascript environment must be created for the purpose of evaluating the result of the string. Not only is that a major performance hit, it’s also a wasteful use of eval due to it providing no benefit over that of correctly assigning the function instead.

There are also security reasons too, such as if the string is not completely trusted.

Documentation sites such as Mozilla’s javascript reference for setTimeout help to make this clear too.

code in the alternate syntax, is a string of code you want to execute after delay milliseconds. (Using this syntax is not recommended for the same reasons as using eval())