Richard Castera

Application Developer/Designer
  • Home
  • About
  • Projects

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!

var _st = window.setTimeout;
window.setTimeout = function(fRef, mDelay) {
	if(typeof fRef == "function") {
		var argu = Array.prototype.slice.call(arguments,2);
		var f = (function(){ fRef.apply(null, argu); });
		return _st(f, mDelay);
	}
	return _st(fRef,mDelay);
}

Define your function to call with setTimeout:

function alertMe(message, name) {
    alert(message + name);
}

Now, just call the function.

var message = 'Hello, ';
var name = 'Richard';
setTimeout(alertMe, 2000, message, name);

Hope this helps someone! :)

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Don't forget to Subscribe and Follow!

Be sure to subscribe to the feed and follow me on Twitter for more insights and resources!

8 Responses to “Javascript – Passing Multiple Parameters through setTimeout”

  1. Comment #1 - Permalink
    harald
    06/24/2009

    why would anyone like to overwrite the setTimeout method, when it’s so much easier to just write:

    var message = ‘Hello’;
    var name = ‘Richard’;

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

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

  2. Comment #2 - Permalink
    Richard
    06/28/2009

    Hello Harald! Thank you for your comment. Sometimes it may be necessary to override native functions to add additional functionality or to implement something specific to your requirements. Your example is missing one major element that this post contains. The ability to pass multiple parameters to a function that is called in setTimeout at the specified interval. With the native setTimeout this is not possible.

  3. Comment #3 - Permalink
    Richard
    10/30/2009

    THIS IS JUST AWESOME! THANKS!!!

  4. Comment #4 - Permalink
    Richard
    11/01/2009

    @Richard, Thanks!

  5. Comment #5 - Permalink
    harry
    04/04/2010

    Thanks a lot !!!
    I was looking for passing multipple args to setTimeout for hours. Now, it’s works !!!

  6. Comment #6 - Permalink
    Zlatan Halilovic
    05/26/2010

    Wow dude. You saved me from having a lot of headaches. Thanks!

  7. Comment #7 - Permalink
    Richard
    05/31/2010

    @Zlatan, Thanks!

  8. Comment #8 - Permalink
    Nizzy
    07/17/2010

    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?

    function myFunction() {
    var args = Array.prototype.slice.call(arguments);
    args.pop();
    alert(args.join(‘ ‘));
    }

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

It's Your turn

Click here to cancel reply.
You

CAPTCHA Image
CAPTCHA Audio
Refresh Image



About The Author

Richard is a Web Developer currently working for SankyNet. He is available for freelance work. Visit Shifting Ideas for inquiries. Thanks!
  • Premium Email Templates

    Social Profiles

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google
  • GitHub

    Last Tweet

  • Beware the heat-seeking Nerf machine-gun coming to a cubicle near you http://om.ly/svHF #fb
  • Follow me
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • September 2008
    • August 2008
    • July 2008
    • June 2008
  • Categories

    • AJAX
    • Apache
    • Books
    • Drupal
    • Ecommerce
    • Flash
    • Google
    • Javascript
    • jQuery
    • Magento
    • Marketing
    • Mootools
    • My Thoughts
    • MySQL
    • News
    • Photoshop
    • PHP
    • Prototype
    • SEO
    • Web Resources
    • Wordpress
  • My Favorites

    • 37 Signals
    • Andrew Warner
    • Chris Coyier
    • Chris Shiflett
    • Collis Ta'eed
    • Development Seed
    • Drupal
    • HubSpot
    • James Padolsey
    • Joel On Software
    • John Resig
    • jQuery
    • Kevin Rose
    • Life Hacker
    • Magento
    • Mashable
    • Matt Cutts
    • Matt Ryan
    • MySQL
    • NetTuts
    • Noupe
    • Photoshop Tutorials
    • PHP
    • Smashing Magazine
    • Tech Crunch
    • WoorkUp
    • Wordpress

2010 © Copyright. Richard Castera - All Rights Reserved.

Top