javascript - Cant run code and function call passed as string inside settimeout -
i want run code using settimeout. have function:
function passing_functions(thefunction) { var my_function ="res="+thefunction+ "(); if (res==true){another_function} " and on call :
passing_functions("load_database"); i have string:
res = load_database();if (res==true){another_function} ok, i'm unable use inside settimetout
settimeout(function() {my_funtion},100} settimeout(function() {my_funtion()},100} settimeout(eval(my_funtion),100} settimeout(my_funtion),100} settimeout(my_funtion()),100} etc --- have error or nothing....
also have tried use "this." prefix "thefunction" without success.
could me ? i'm doing wrong ? thanks
note: (i want create array of things executed. use passing_functions(load_database); receive code instead function. because i'm using string pass code.
)
all "function calls" end } instead of ) or have closing ) somewhere in between. the signature settimeout(func, time), i.e. function accepts 2 arguments, function , number. arguments put between (...) , separated commas. assuming my_funtion function, settimeout(my_funtion, 100) valid.
however seems trying run javascript code inside string. don't that. javascript powerful language functions first class citizens. instead of passing name of function , build string, pass function directly:
function passing_functions(thefunction) { settimeout( function() { // first argument: function var res = thefunction(); // call function passed argument if (res) { // assume want *call* function // putting another_function there doesn't another_function(); } }, 100 // second argument: delay ); } // function load_database must exist @ time pass passing_functions(load_database); whether or not want cannot say, should give idea how solve issue.
Comments
Post a Comment