javascript - Make a function wait for another variable -
i've gone project lately requires 2 different methods 2 different variable, need store both of these values in order use them in function.
it's bit complicated, let me explain. i've got switch statement watches several numbers: 2,3,13,45
var hello; switch ($variable) { case 2: //do irrelevant break; case 3: //do irrelevant break; case 13: //do variable named "hello". ex.: alert(hello); break; case 45: hello = "hello"; break; }
as can see, need value of "hello", before variable gets value. can't change order, nor can't change variable declaration, have setup.
what thought make function call in 13:
function getmyvariable(variable) { var v = variable; return variable; } case 13: getmyvariable(hello); break;
then of course function still return undefined.
what i'd do, make function wait variable set in 45:
(note speculation, code no way near working)
case 45: hello = "hello"; getmyvariable(hello); break; function getmyvariable(variable) { //function gets call case 13 //if type undefined, wait variable isn't undefined //if variable isn't undefined return variable }
so @ first call skip case 13, let case 45 set variable, go case 13 , execute code here. can follow me? tell me if need more info!
// needs defined somewhere outside preserved // between calls code contains `switch` var dothiswhenhelloisset = []; // ... var hello; switch ($variable) { // ... case 13: dothiswhenhelloisset.push(function(h) { // argument named "h" // (which have value of variable named "hello"). // ex.: alert(h); }); break; case 45: hello = "hello"; (var = 0; < dothiswhenhelloisset.length; i++) { dothiswhenhelloisset[i](hello); } dothiswhenhelloisset = []; break; }
note: if want store 1 next action, not need array.
Comments
Post a Comment