Tuesday, April 5, 2011

Pass parameters in setInterval function

How to pass parameter while call a function using setInterval. viz. setInterval('funca(10,3)',500); is incorrect.

From stackoverflow
  • You need to create an anonymous function so the actual function isn't executed right away.

    setInterval( function() { funca(10,3); }, 500 );
    
    Rakesh : yep. silly me. I could have thought of that ! Thanks buddy
  • Add them as parameters to setInterval:

    setInterval(funca,500,10,3);
    
    Kev : This will not work on Internet Explorer.
    Crescent Fresh : Whaa?! Since when was that allowed? (serious question)
    Kev : Not sure. My source was: https://developer.mozilla.org/en/DOM/window.setInterval
  • You can use an anonymous function;

    setInterval(function() { funca(10,3); },500);
    
  • This is not exactly the same parameters as I read here: http://www.w3schools.com/jsref/met_win_setinterval.asp

    I'm a bit confused ...

0 comments:

Post a Comment