SetTimeout后获取返回值

发布于 2024-10-20 22:24:55 字数 670 浏览 6 评论 0原文

我刚刚询问过有关按名称调用函数的问题,现在我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-10-27 22:24:55

您不需要使用任何此类字符串操作。只需将函数引用传递给 window.setTimeout() 即可。要存储函数的返回值,只需将其分配给传递给 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);
}

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);
}
天暗了我发光 2024-10-27 22:24:55

如果您想从 ECall 返回一个值,这是行不通的。

setTimeout 是异步的,这意味着 ECall 将在调用 setTimeout 代码之前返回。

或者,如果您希望 alert() 成为 setTimeout 的一部分,您可以传递一个匿名函数。另外,最好不要将字符串传递给 setTimeout

我会这样做:

function ECall(funcName, arg)
{
       // Get an Array of the arguments, except for the first one.
    var args = Array.prototype.slice.call( arguments, 1 );

       // Pass an anonymous function that calls the global "funcName" function
       //    inside the alert(). 
       // It uses .apply() to pass the arguments we sliced.
    setTimeout( function() {
        alert( window[ funcName ].apply( window, args ) );
    }, 1000);
}

If you wanted to return a value from ECall, it won't work.

The setTimeout is asynchronous, which means that the ECall will return before the setTimeout code is invoked.

Or if you wanted the alert() to be part of the setTimeout, you could pass an anonymous function. Also, it would be better to not pass a string to the setTimeout.

I'd do this instead:

function ECall(funcName, arg)
{
       // Get an Array of the arguments, except for the first one.
    var args = Array.prototype.slice.call( arguments, 1 );

       // Pass an anonymous function that calls the global "funcName" function
       //    inside the alert(). 
       // It uses .apply() to pass the arguments we sliced.
    setTimeout( function() {
        alert( window[ funcName ].apply( window, args ) );
    }, 1000);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文