Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > javascript to execute ajax function every 30 seconds

Reply
Thread Tools

javascript to execute ajax function every 30 seconds

 
 
inetquestion
Guest
Posts: n/a
 
      07-09-2008
How do you enable javascript to execute a given function every X
seconds, which is not dependant on any user input?

-Inet
 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      07-09-2008
setInterval(yourfunction,milliseconds);
 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      07-12-2008
On Jul 9, 11:24 am, Peter <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      07-12-2008
In comp.lang.javascript message <ab5db72a-bd6c-48ae-8ba2-5b01b0937cbb@34
g2000hsf.googlegroups.com>, Sat, 12 Jul 2008 10:20:32, Peter Michaux
<(E-Mail Removed)> posted:
>
>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.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
 
Reply With Quote
 
seani
Guest
Posts: n/a
 
      07-15-2008


Peter Michaux wrote:
> On Jul 9, 11:24 am, Peter <(E-Mail Removed)> wrote:
> > 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


A tiny point of order / question: the call to setTimeout isn't
actually recursive here, is it?
 
Reply With Quote
 
Henry
Guest
Posts: n/a
 
      07-15-2008
On Jul 15, 1:47*pm, seani wrote:
> Peter Michaux wrote:

<snip>
>> function foo() {
>> * // do stuff
>> * // ...

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

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


No it is not.
 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      07-15-2008
On Jul 15, 6:06 am, Henry <(E-Mail Removed)> wrote:
> On Jul 15, 1:47 pm, seani wrote:
>
> > Peter Michaux wrote:

> <snip>
> >> function foo() {
> >> // do stuff
> >> // ...

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

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

>
> 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
 
Reply With Quote
 
seani
Guest
Posts: n/a
 
      07-15-2008
On 15 Jul, 15:22, Peter Michaux <(E-Mail Removed)> wrote:
> On Jul 15, 6:06 am, Henry <(E-Mail Removed)> wrote:
>
>
>
> > On Jul 15, 1:47 pm, seani wrote:

>
> > > Peter Michaux wrote:

> > <snip>
> > >> function foo() {
> > >> * // do stuff
> > >> * // ...

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

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

>
> > 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.
>


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).
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-15-2008
Peter Michaux wrote:
> On Jul 15, 6:06 am, Henry <(E-Mail Removed)> wrote:
>> On Jul 15, 1:47 pm, seani wrote:
>>> Peter Michaux wrote:

>> <snip>
>>>> function foo() { // do stuff // ... // make a delayed recursive
>>>> call to foo setTimeout(foo, 3000); }

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

>> No it is not.

>
> 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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to execute a method every few seconds Zoe Phoenix Ruby 4 04-15-2008 02:35 AM
How to convert Seconds to X Hours Y Minutes Z Seconds? 00_CP_D12 ASP .Net 3 02-22-2008 10:37 AM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Convert seconds to minutes and seconds tshad ASP .Net 7 03-11-2005 11:27 PM
Converting seconds to (Days, Hours, Minutes, seconds) Stu C Programming 7 03-07-2005 08:44 AM



Advertisments