#1
  1. PHP is my Love!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Kafr Sakr, Egypt
    Posts
    87
    Rep Power
    11

    SetTimeout arguments problem


    Hello,
    As we know that setTimeout() is used to call a function spontanousely over fixed parts of time as the following manner:
    setTimeout(myFunc,1000);
    The problem I got is, I want to call the function myFunc() with passing an argument to it as follows,
    setTimeout(myFunc('parameter'),1000);
    The above code generates an error. So how can I get my goal?
    Best Regards,
    Said Bakr.
  2. #2
  3. No Profile Picture
    JackOfAllTrades, MasterOfNone
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Kentucky, USA
    Posts
    398
    Rep Power
    124
    setTimeout() and setInterval() can take one of two things for their first argument. The first is, as you said, a function reference. The second is a string which gets eval()'ed. So in your example, you'd want to do this:
    javascript Code:
    setTimeout("myFunc('parameter');", 1000);

    If using variables as parameters to a delayed function, keep in mind the following distinction:
    javascript Code:
    // calls myFunc() with the value param will have 1000ms from now
    setTimeout("myFunc(param);", 1000);
     
    // calls myFunc() with the current value of param,
    // even if it changes by the time myFunc() actually gets called
    setTimeout("myFunc(" + param + ");", 1000);

    Comments on this post

    • wsa1971 agrees
    Last edited by jnsg; May 2nd, 2006 at 04:33 PM.
  4. #3
  5. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    604
    Furthermore, it depends on the nature of your parameter, because the expresion is evaluated.

    1. If the parameter is a number or a stringed number
    var param =5
    or
    var param ='5'

    and you need to pass it as a decimal number
    the syntax is:
    PHP Code:
    setTimeout('myFunc('+param+')',3000
    2. But if the parameter is an alphanumeric

    var param ='foo';
    the parameter has to be quoted (to avoid evaluation) and the syntax becomes:
    PHP Code:
    setTimeout('myFunc("'+param+'")',3000
    In the first case as the parameter is evaluated, even if the variable is in fact a string, such as '5', when passed it becomes a decimal number.

    Comments on this post

    • wsa1971 agrees
    Last edited by KorRedDevil; May 4th, 2006 at 06:56 AM.
  6. #4
  7. PHP is my Love!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Kafr Sakr, Egypt
    Posts
    87
    Rep Power
    11

    Conclusion


    From the above threads, I may be able to conclude that:-
    setTimeout() method, acts as eval() method in addition, it able to repeat the evaluation of the string every determined period of time passed.
    Best Regards,
    Said Bakr.
  8. #5
  9. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    604
    Quite so, except that you may avoid evaluation if you quote the parameter.

IMN logo majestic logo threadwatch logo seochat tools logo