Results 1 to 3 of 3

Thread: Question about setTimeout

  1. #1
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default Question about setTimeout

    Just, a question, but when a function has arguments how come you can't do setTimeout?

    Example:
    Code:
    function getCode(arg1,arg2) {
    //code
    setTimeout("getCode()",100);
    }
    and when you call it in the body, it won't work? I'm really confused as to how to do this. Thanks in advance
    - Mike

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I even tried escaping the objects like this:
    Code:
    setTimeout("getCode(\""+arguments[0]+"\",\""+arguments[1]+"\")")
    And still no good.
    - Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code passed as a string to setTimeout() or setInterval() is evaluated in the global scope. Create a closure:
    Code:
    setTimeout(function() { getCode(arg1, arg2); }, 100);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •