SetTimeout后获取返回值
我刚刚询问过有关按名称调用函数的问题,现在我想在 SetTimeout
之后处理 return
语句:
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
效果很好,但我必须保存被调用函数的返回值。当我写: setTimeout('alert(window[\'' + funcName + '\']' + command + ')', 1000);
它提醒函数的返回值。我该如何保存它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要使用任何此类字符串操作。只需将函数引用传递给
window.setTimeout()
即可。要存储函数的返回值,只需将其分配给传递给window.setTimeout()
的函数中的变量即可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 towindow.setTimeout()
如果您想从
ECall
返回一个值,这是行不通的。setTimeout
是异步的,这意味着ECall
将在调用setTimeout
代码之前返回。或者,如果您希望
alert()
成为setTimeout
的一部分,您可以传递一个匿名函数。另外,最好不要将字符串传递给setTimeout
。我会这样做:
If you wanted to return a value from
ECall
, it won't work.The
setTimeout
is asynchronous, which means that theECall
will return before thesetTimeout
code is invoked.Or if you wanted the
alert()
to be part of thesetTimeout
, you could pass an anonymous function. Also, it would be better to not pass a string to thesetTimeout
.I'd do this instead: