Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  47] [ 4]  / answers: 1 / hits: 15523  / 14 Years ago, sun, march 6, 2011, 12:00:00

I've just asked about calling functions by name, now I want to process return statement after SetTimeout:



function ECall(funcName, arg)
{
command += (;
for (var i=1; i<arguments.length; i++)
{
command += ' + arguments[i] + ';
if (i != arguments.length-1) command += ',';
}
command += );

//var funcPtr = eval(funcName);
//return funcPtr(arg); // This works, but I need SetTimeout

setTimeout('window['' + funcName + '']' + command, 1000);
}


setTimeout works great, but I have to save return value of called function. When I write: setTimeout('alert(window['' + funcName + '']' + command + ')', 1000);
It alerts return value of function. How can I store it?


More From » function

 Answers
5

You don't need to use any of this string manipulation. Just pass a function reference to window.setTimeout(). To store the returned value of the function, simply assign it to a variable in the function you pass to window.setTimeout()



var savedValue;

function ECall(funcName)
{
var args = Array.prototype.slice.call(arguments, 1);
var func = window[funcName];

window.setTimeout(function() {
savedValue = func.apply(this, args);
}, 1000);
}

[#93409] Friday, March 4, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
catrinas

Total Points: 587
Total Questions: 100
Total Answers: 105

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;