javascript to execute ajax function every 30 seconds

Discussion in 'Javascript' started by inetquestion, Jul 9, 2008.

  1. inetquestion

    inetquestion Guest

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

    -Inet
     
    inetquestion, Jul 9, 2008
    #1
    1. Advertising

  2. inetquestion

    Peter Guest

    setInterval(yourfunction,milliseconds);
     
    Peter, Jul 9, 2008
    #2
    1. Advertising

  3. On Jul 9, 11:24 am, Peter <> 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
     
    Peter Michaux, Jul 12, 2008
    #3
  4. In comp.lang.javascript message <ab5db72a-bd6c-48ae-8ba2-5b01b0937cbb@34
    g2000hsf.googlegroups.com>, Sat, 12 Jul 2008 10:20:32, Peter Michaux
    <> 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.
     
    Dr J R Stockton, Jul 12, 2008
    #4
  5. inetquestion

    seani Guest

    Peter Michaux wrote:
    > On Jul 9, 11:24 am, Peter <> 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?
     
    seani, Jul 15, 2008
    #5
  6. inetquestion

    Henry Guest

    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.
     
    Henry, Jul 15, 2008
    #6
  7. On Jul 15, 6:06 am, Henry <> 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
     
    Peter Michaux, Jul 15, 2008
    #7
  8. inetquestion

    seani Guest

    On 15 Jul, 15:22, Peter Michaux <> wrote:
    > On Jul 15, 6:06 am, Henry <> 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).
     
    seani, Jul 15, 2008
    #8
  9. Peter Michaux wrote:
    > On Jul 15, 6:06 am, Henry <> 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>
     
    Thomas 'PointedEars' Lahn, Jul 15, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    7
    Views:
    17,067
    tshad
    Mar 11, 2005
  2. 00_CP_D12
    Replies:
    3
    Views:
    8,972
    dilipv
    Feb 22, 2008
  3. Stu
    Replies:
    7
    Views:
    1,785
    Dave Thompson
    Mar 7, 2005
  4. Michael Tan
    Replies:
    32
    Views:
    1,021
    Ara.T.Howard
    Jul 21, 2005
  5. Zoe Phoenix
    Replies:
    4
    Views:
    133
    Zoe Phoenix
    Apr 15, 2008
Loading...

Share This Page