[英]how to wait with setTimeout until a variable get loaded and, at the same time, receive HTTP requests!
如果加載了全局變量,我已經在JavaScript函數中檢查每100毫秒。 當加載變量時,函數將返回變量的值,如下所示。 在我的代碼中,我在JavaScript中使用HTTP服務器,當具有特定標頭的特定HTTP請求到達我的服務器時,將加載該變量。
function checkVariable()
{
if ( myvar != null )
{
return myVar;
}
else
{
window.setTimeout("checkVariable();",100);
}
}
我在一段代碼中使用此函數,如下所示:
// arithmetis operations... [1]
myVar = checkVariable();
// arithmetic operations that use myVar [2]
myVar以null啟動。 問題是[2]中的算術運算是在myVar得到它的值之前完成的。 相反,我希望我的代碼等到myVar得到它的值,然后繼續操作。
在嘗試setTimeout函數之前,我嘗試使用while循環使代碼等待,但問題是HTTP服務器由於連續執行while循環而無法接收任何HTTP請求!
有人可以幫我解決這個問題嗎?
先感謝您!
我可能會將剩余的算術運算作為回調。 就像是:
function checkVariable()
{
if ( myvar != null )
{
computeVariable(myVar);
}
else
{
window.setTimeout("checkVariable();",100);
}
}
然后:
// arithmetis operations... [1]
myVar = checkVariable();
function computeVariable(myVar) {
// arithmetic operations that use myVar [2]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.