javascript to execute ajax function every 30 seconds

I

inetquestion

How do you enable javascript to execute a given function every X
seconds, which is not dependant on any user input?

-Inet
 
P

Peter Michaux

setInterval(yourfunction,milliseconds);

Please quote the message to which you are replying or state enough so
your message is sufficient to explain the context of your response.

----

Using setInterval to execute a given function every x seconds can be
dangerous. If the function's execution takes longer than x seconds
than the executions of the function "stack up" and dominate the system
because they are always executing.

I prefer to use setTimeout so that there is a specified period of time
between the end of the function's execution and the beginning of its
next execution.

function foo() {
// do stuff
// ...

// make a delayed recursive call to foo
setTimeout(foo, 3000);
}

Peter
 
D

Dr J R Stockton

In comp.lang.javascript message <ab5db72a-bd6c-48ae-8ba2-5b01b0937cbb@34
g2000hsf.googlegroups.com>, Sat, 12 Jul 2008 10:20:32, Peter Michaux
I prefer to use setTimeout so that there is a specified period of time
between the end of the function's execution and the beginning of its
next execution.

function foo() {
// do stuff
// ...

// make a delayed recursive call to foo
setTimeout(foo, 3000);
}

And one can call new Date() within function foo and from it calculate
a suitably-varied delay to replace the fixed 3000.
 
S

seani

Peter said:
Please quote the message to which you are replying or state enough so
your message is sufficient to explain the context of your response.

----

Using setInterval to execute a given function every x seconds can be
dangerous. If the function's execution takes longer than x seconds
than the executions of the function "stack up" and dominate the system
because they are always executing.

I prefer to use setTimeout so that there is a specified period of time
between the end of the function's execution and the beginning of its
next execution.

function foo() {
// do stuff
// ...

// make a delayed recursive call to foo
setTimeout(foo, 3000);
}

Peter

A tiny point of order / question: the call to setTimeout isn't
actually recursive here, is it?
 
P

Peter Michaux

No it is not.

If we are nit picking, foo is defined in terms of itself so it is a
recursively defined function. It does not evolve a recursive process,
however, as the return path is not growing with each call to foo in
the cycle.

Peter
 
S

seani

If we are nit picking, foo is defined in terms of itself so it is a
recursively defined function. It does not evolve a recursive process,
however, as the return path is not growing with each call to foo in
the cycle.

Well, nitpicking wasn't the intention :)

I've used setTimeout in exactly the manner suggested for a number or
reasons and wanted to be certain I wasn't missing some behaviour that
would catch me out in long-running / short timeout scenarios.

But to give nitpicking a spin for a bit, I didn't refer to the
definition of foo itself, but explicitly to the nature of the call in
the body of the function. These seem like different things to me, but
is that ignorance on my part (always a fair bet).
 
T

Thomas 'PointedEars' Lahn

Peter said:
If we are nit picking, foo is defined in terms of itself so it is a
recursively defined function.
Nonsense.

It does not evolve a recursive process, however, as the return path is
not growing with each call to foo in the cycle.

The correct explanation for non-recursiveness in any way is that `foo' is
but a reference to a Function object that is passed to setTimeout(), which
is [[Call]]ed in the global execution context of the view after the
specified number of milliseconds.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top