SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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:
    Code:
    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.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    setTimeout only occurs once, you need to use setInterval

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Code:
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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();
    }

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Non-Member
    Join Date
    Dec 2010
    Location
    /home/pc
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another option

    Code JavaScript:
    window.onload = init;
     
    function init() {
       //do other stuff here
       function_to_call();
    }
     
    function function_to_call() {
        //do something
        setTimeout(function_to_call, 2000);
    }

  7. #7
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    function init() {
    setInterval("function_to_call()", 1000);
    }
    Sorry! that should of course have been

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

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AllanP View Post
    Sorry! that should of course have been

    function init() {
    setTimeout("function_to_call()", 1000);
    }
    NO. NO. NO. It needs to be:

    Code:
    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".
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by AllanP View Post
    I beg to differ here. The correct syntax is either
    setTimeout("script Expression",msecs[, language]) or,
    setTimeout(functionReference,msecs[, arg1, ...argN])
    There are though significant reasons why one is preferred over the other.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    Non-Member
    Join Date
    Dec 2010
    Location
    /home/pc
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  12. #12
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I am curious. What are these significant and preferred reasons?

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by AllanP View Post
    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())
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •