setTimeout的后返回变量 [英] Return variable after setTimeout

查看:224
本文介绍了setTimeout的后返回变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只返回后,它已被一个setTimeout的回调中设置一个变量。我想不出任何其他方式做到这一点,但这里是我的尝试(我知道code似乎很傻,但它是解决一个错误科尔多瓦)。对于超出我的理解的原因,它会导致一个无限循环。

 函数isConnected(){
    VAR networkState = navigator.connection.type;    VAR做= FALSE;
    的setTimeout(函数(){
        networkState = navigator.connection.type;
        做= TRUE;
    },500);    而(!完成){}
    返回networkState == Connection.NONE!;
}

任何人都可以向我解释为什么发生这种情况,或者提供替代的?

解决方案:

 函数isConnected(回调){
    navigator.connection.type;
    的setTimeout(函数(){
        回调(navigator.connection.type == Connection.NONE!);
    },500);
}
isConnected(功能(连接){
    如果(!连)
        警报('不连接');
});


解决方案

您根本就不能解决问题的方式。因为JavaScript是单线程的,当执行你的JS螺纹与无限循环完成这样的setTimeout回调只能运行,它永远不会得到运行和你的code将挂起。最终,浏览器会抱怨一个长期运行的JS块,并提供将其关闭。

相反,你需要使用的的setTimeout()的回调函数,并从该回调继续你的code。您无法使用顺序code为超时。您必须使用异步编程风格。

您可以做这样的事情,你在回调中传递并调用执行回调并传递连接的布尔值:

 函数GetConnectedState(回调){    的setTimeout(函数(){
        回调(navigator.connection.type == Connection.NONE!);
    },500);}

您现有的code似乎并没有提供比在1/2秒一看连接状态的任何其他。在JavaScript中异步操作的任何真正的排序(如Ajax调用或WebSocket连接),也应该有一个成功或完成回调会告诉你,如果该操作实际完成的时候/你也可以触发基于回调该(早于1/2秒的大部分时间)。所以,这并没有真正看起来像一个完整的解决方案,但它的所有你告诉我们,你正在使用。

例如,这里有一个答案,你可以看看调用回调成功时的图像加载,有错误或者超时没有响应:<一href=\"http://stackoverflow.com/questions/9714525/javascript-image-url-verify/9714891#9714891\">Javascript图像URL验证和<一个href=\"http://stackoverflow.com/questions/8778718/how-to-implement-a-function-timeout-in-javascript-not-just-the-settimeout/8778899#8778899\">How实现&QUOT;功能超时&QUOT;在Javascript中 - 不仅仅是'setTimeout的。类似的概念可以用于一些其他类型的异步调用的实现自己的超时时间。

I'm trying to return a variable only after it has been set within a setTimeout callback. I couldn't think of any other way to do this, but here's my attempt (I know the code seems silly, but it's to work around a Cordova bug). For reasons beyond my understanding, it results in an infinite loop.

function isConnected() {
    var networkState = navigator.connection.type;

    var done = false;
    setTimeout(function() {     
        networkState = navigator.connection.type;
        done = true;
    }, 500);

    while (!done) {}
    return networkState !== Connection.NONE;
}

Can anyone explain to me why this is happening, or provide an alternative?

Solution:

function isConnected(callback) {
    navigator.connection.type;
    setTimeout(function() {     
        callback(navigator.connection.type !== Connection.NONE);
    }, 500);
}


isConnected(function(connected) {
    if (!connected)
        alert('Not Connected');
});

解决方案

You simply can't solve your problem that way. Because javascript is single threaded, the setTimeout callback can only run when your JS thread of execution finishes so with your infinite loop, it will never get to run and your code will hang. Eventually the browser will complain about a long-running piece of JS and offer to shut it down.

Instead, you need to use a callback function on the setTimeout() and continue your code from that callback. You can't use sequential code for timeouts. You must use asynchronous coding styles.

You could do something like this where you pass in a callback and it calls the callback back and passes it the connected boolean:

function GetConnectedState(callback) {

    setTimeout(function() {     
        callback(navigator.connection.type !== Connection.NONE);
    }, 500);

}

Your existing code doesn't seem to offer anything other than a look at the connection state in 1/2 second. With any real sort of asynchronous operation in javascript (such as an ajax call or a websocket connection), there should also be a success or completion callback that will tell you when/if the operation actually completes and you can also trigger the callback based on that (sooner than 1/2 second most of the time). So, this doesn't really look like a complete solution, but it's all you show us you're using.

For example, here's an answer you can look at that calls a callback when an images loads successfully, with an error or times out with no response: Javascript Image Url Verify and How to implement a "function timeout" in Javascript - not just the 'setTimeout'. A similar concept could be used for implementing your own timeout on some other type of asynchronous call.

这篇关于setTimeout的后返回变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆