最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to wait with setTimeout until a variable get loaded and, at the same time, receive HTTP requests! - Stack Overf

programmeradmin10浏览0评论

I' ve made a in JavaScript function to check every 100 ms if a global variable is loaded. When the variable will be loaded the function will return the value of the variable as shown below. In my code I use an HTTP server in JavaScript, and the variable will be loaded when a specific HTTP request with specific headers arrive to my server.

function checkVariable()
{
    if ( myvar != null )
    {
            return myVar;
    }
    else
    {
            window.setTimeout("checkVariable();",100);
    }
} 

I use this function in a piece of code like this:

// arithmetis operations... [1]

myVar = checkVariable();

// arithmetic operations that use myVar [2]

myVar is initiated with null. The problem is that the arithmetic operations in [2] are done before myVar got its value. Instead, I want my code to wait until myVar get its value, and then to continue with the operations.

Before trying the setTimeout function, I tried to make the code waiting using a while loop, but the problem then was that the HTTP server couldn't receive any HTTP request due to the continuously execution of the while loop!

Could someone help me to solve this problem?

Thank you in advance!

I' ve made a in JavaScript function to check every 100 ms if a global variable is loaded. When the variable will be loaded the function will return the value of the variable as shown below. In my code I use an HTTP server in JavaScript, and the variable will be loaded when a specific HTTP request with specific headers arrive to my server.

function checkVariable()
{
    if ( myvar != null )
    {
            return myVar;
    }
    else
    {
            window.setTimeout("checkVariable();",100);
    }
} 

I use this function in a piece of code like this:

// arithmetis operations... [1]

myVar = checkVariable();

// arithmetic operations that use myVar [2]

myVar is initiated with null. The problem is that the arithmetic operations in [2] are done before myVar got its value. Instead, I want my code to wait until myVar get its value, and then to continue with the operations.

Before trying the setTimeout function, I tried to make the code waiting using a while loop, but the problem then was that the HTTP server couldn't receive any HTTP request due to the continuously execution of the while loop!

Could someone help me to solve this problem?

Thank you in advance!

Share Improve this question asked Jul 11, 2010 at 18:03 Thanasis PetsasThanasis Petsas 4,4485 gold badges33 silver badges57 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 13

I would probably make the remaining arithmetric operations a callback. Something like:

function checkVariable()
{
    if ( myvar != null )
    {
            computeVariable(myVar);
    }
    else
    {
            window.setTimeout("checkVariable();",100);
    }
} 

Then:

// arithmetis operations... [1]

myVar = checkVariable();

function computeVariable(myVar) {
  // arithmetic operations that use myVar [2]
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论