Javascript – Passing Multiple Parameters through setTimeout

One of the problems that I’ve faced with Javascript is passing multiple parameters through the setTimeout function. I found this little snippet on the internet a few months ago and would like to share it with you. I don’t know who the original author is so I could not give him/her proper credit. Thanks!

Define your function to call with setTimeout:

Now, just call the function.

Hope this helps someone! 🙂

13 thoughts on “Javascript – Passing Multiple Parameters through setTimeout”

  1. why would anyone like to overwrite the setTimeout method, when it’s so much easier to just write:{syntaxhighlighter brush: javascript}

    var message = ‘Hello’;

    var name = ‘Richard’;

    window.setTimeout(function() { alertMe(message, name); }, 2000);

    {/syntaxhighlighter}… am i missing something or did the original author of the snippet did not understand variable scope in javascript?

  2. Hi Richard, thanks for sharing the code. It looks nice, but I think the following code can do this job easier. Don’t you think?

    {syntaxhighlighter brush: javascript}

    function myFunction() {

    var args = Array.prototype.slice.call(arguments);

    args.pop();

    alert(args.join(‘ ‘));

    }

    setTimeout(myFunction,1000,’1′,’2′,’3′,’4′,’5′,’6′,’7’);

    {/syntaxhighlighter}

  3. Thank you! This solution saved my day – been trying to overcome a problem of dynamically creating elements and attaching waits to them – the element waits need to remember the current variable states on execution. After hours I found this – thanks! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *