Javascript Callback when variable is set to X

Posted by Erik on Stack Overflow See other posts from Stack Overflow or by Erik
Published on 2010-05-29T18:29:04Z Indexed on 2010/05/29 18:32 UTC
Read the original article Hit count: 262

Filed under:
|

Hey everyone,

Have an issue I can't seem to wrap my head around. I'm wanting to write a generic javascript function that will accept a variable and a callback, and continue to execute until that variable is something other than false.

For example, the variable SpeedFeed.user.sid is false until something else happens in the code, but I don't want to execute a particular callback until it has been set.

The call:

SpeedFeed.helper_ready(SpeedFeed.user.sid, function(){
  alert(SpeedFeed.user.sid);
  // Run function that requires sid to be set.
});

The function:

helper_ready: function(vtrue, callback){
  if(vtrue != false){
    callback();
  } else {
    setTimeout(function(){
       SpeedFeed.helper_ready(vtrue, callback);
    }, SpeedFeed.apiCheckTime);
  }
}

The issue I've narrowed it down to appears to be that because in the setTimeout I call vtrue instead of the actual SpeedFeed.user.sid, it's going to be set to false always. I realize I could write a specific function for each time that just evaluates the SpeedFeed.user.sid, but I'd like to have a generic method that I could use throughout the application.

Thanks for any insight :)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery