Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Passing paramiters to setTimeout

The dialect of Java Script interpreted by Titanium Mobiles runtime is not allowing parameters to be passed to setTimeout functions.

Can anyone help with best practices on setTimout parameter passing.

This works on my web pages but not in Titanium on iPhone, iPad, or Android:

window.setTimeout(function (a,b) {
  //do something with a and b
},10,someString,someObject);
— asked July 23rd 2010 by Tom Campbell
  • ipad
  • iphone
  • java
  • mobile
  • script
  • settimout
  • timer
0 Comments

3 Answers

  • I feel like perhaps I'm misunderstanding your question, but this works fine for me:

    function myFunc(myArg) {
        Titanium.API.info(myArg);
    }
    
    var thing = 'some value';
    scrollTimeout = setTimeout(function() {myFunc(thing);}, 100);
    

    That outputs 'some value' in the log window - is that the kind of thing you're after?

    Toby

    — answered October 8th 2010 by Toby Mathews
    permalink
    1 Comment
    • In our cases var thing will already be changed to a different value by the time 100 ms passes. This is why we need to pass the value of thing, rather than simply getting the value of var thing after the 100 ms has passed. Make sense?

      — commented October 12th 2010 by Nick Robillard
  • Not sure if this is the correct context but this is the only way I've been able to get setTimeout to work..although I've never had to pass parameters but I don't see why it wouldn't work.

    Hope it helps!

    setTimeout(function()
    {
      yourFunction(a,b);
    }, 1000 );
    
    — answered July 24th 2010 by Kelly Redd
    permalink
    1 Comment
    • Thanks I just tested this but my params had changed by the time the setTimiout() calls my function. I need to pass params in that will be passed to the function and not be modified. I have several timeout running at any moment each with a different set of params.

      — commented July 24th 2010 by Tom Campbell
  • No answers? :S

    — answered October 8th 2010 by Nick Robillard
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.