想在javascript中设置延迟

时间:2021-01-13 01:19:08

I want to set delay in javascript code so that XML file generated before running of javascript . Here is my html code

我想在javascript代码中设置延迟,以便在运行javascript之前生成XML文件。这是我的HTML代码

<body onLoad="Func1Delay()">
<div id="map"></div>
</body>

In this Func1Delay() function i have written code to delay execution of javascript

在这个Func1Delay()函数中,我编写了代码来延迟执行javascript

function Func1Delay()
    {
    setTimeout("load()", 3000);
    }

load() is javascript function ? how can i delay execution of javascript code so that xml file successfully generated before code execution??

load()是javascript函数吗?我怎样才能延迟执行javascript代码,以便在代码执行之前成功生成xml文件?

3 个解决方案

#1


1  

Seems like your downloadUrl function provides a callback. The callback function fires automatically, after the XML is loaded. You do not need a 3 second delay, just move your logic inside the callback function. Something like this:

好像你的downloadUrl函数提供了一个回调。加载XML后,回调函数会自动触发。您不需要3秒延迟,只需在回调函数内移动逻辑。像这样的东西:

function Func1Delay() {
    downloadUrl("location.xml", function (data) {
        var xml = data.responseXML;
        // do any thing with xml, it is loaded!
        // alert(xml);
    });
}

#2


1  

That's how you do it, except you don't want to use a string (although it works — provided you have a function called load defined at global scope). setTimeout schedules a function to be called a given number of milliseconds later.

你就是这样做的,除了你不想使用字符串(虽然它有效 - 只要你有一个名为load在全局范围内定义的函数)。 setTimeout计划一个函数,以便稍后调用给定的毫秒数。

It's better to give it an actual function reference:

最好给它一个实际的函数参考:

function Func1Delay() {
    setTimeout(load, 3000);

    function load() {
        // Stuff to do three seconds later
    }
}

Note that the event you're using to trigger it, the onload of body, already happens really, really late in the page load cycle, and so whatever you're waiting for may already be done; conversely, if it might take more than three seconds, you might not be waiting long enough. So if there's something you can check to see whether it's done or not, you can poll, like this:

请注意,您用于触发它的事件,即正文的负载,已经在页面加载周期中发生了,所以无论您在等待什么,都可能已经完成;相反,如果可能需要超过三秒钟,您可能不会等待足够长的时间。所以,如果有什么东西你可以查看它是否已经完成,你可以像这样进行民意调查:

function Func1Delay() {

    check();

    function check() {
        if (theWorkIsDone) {
            // Do something with the work
        }
        else {
            // Check back in 100ms (1/10th of a second)
            setTimeout(check, 100);
        }
    }
}

#3


1  

You want the function to execute as soon as possible, but in every case after your xml has been successfully generated.

您希望函数尽快执行,但在每次成功生成xml之后都要执行。

In this case you should prevent using a fixed amount of time (because you don't know the value exactly), but try the following:

在这种情况下,您应该防止使用固定的时间(因为您不完全知道该值),但请尝试以下操作:

function load(){

    if (/*check here if the xml has *not yet* been generated*/){
          setTimeout(load,50); // try again in 50 milliseconds
          return;
    }

    // do your stuff here
}

This loops as long as your xml is not ready, and kicks in as soon as it's available.

只要你的xml没有准备好,它就会循环,并在它可用时立即启动。

General about setTimeout:

关于setTimeout的一般信息:

You can pass a string, but this is highly discouraged from for several reasons. Instead pass a function reference or a function like this:

你可以传递一个字符串,但出于几个原因,这是非常不鼓励的。而是传递一个函数引用或这样的函数:

// function reference
setTimeout(load,3000) // no `()` !

// function
setTimeout( function(){load()},3000)

If you need paramters be passed to the function, you can't use the first option but need to use the second one, where you can easily pass them load(params).

如果您需要将参数传递给函数,则不能使用第一个选项,但需要使用第二个选项,您可以轻松地将它们传递给load(params)。

If you pass a function like this: setTimeout(load(),3000) it executes the function load and passes its return value to the timeout. You however want the function invoked after 3 seconds and thus only pass the reference to the function.

如果你传递这样的函数:setTimeout(load(),3000)它执行函数load并将其返回值传递给超时。但是,您希望在3秒后调用该函数,从而仅将引用传递给函数。

Notice however, that you have a different scope if you execute the functions this way.

但请注意,如果以这种方式执行函数,则具有不同的范围。

#1


1  

Seems like your downloadUrl function provides a callback. The callback function fires automatically, after the XML is loaded. You do not need a 3 second delay, just move your logic inside the callback function. Something like this:

好像你的downloadUrl函数提供了一个回调。加载XML后,回调函数会自动触发。您不需要3秒延迟,只需在回调函数内移动逻辑。像这样的东西:

function Func1Delay() {
    downloadUrl("location.xml", function (data) {
        var xml = data.responseXML;
        // do any thing with xml, it is loaded!
        // alert(xml);
    });
}

#2


1  

That's how you do it, except you don't want to use a string (although it works — provided you have a function called load defined at global scope). setTimeout schedules a function to be called a given number of milliseconds later.

你就是这样做的,除了你不想使用字符串(虽然它有效 - 只要你有一个名为load在全局范围内定义的函数)。 setTimeout计划一个函数,以便稍后调用给定的毫秒数。

It's better to give it an actual function reference:

最好给它一个实际的函数参考:

function Func1Delay() {
    setTimeout(load, 3000);

    function load() {
        // Stuff to do three seconds later
    }
}

Note that the event you're using to trigger it, the onload of body, already happens really, really late in the page load cycle, and so whatever you're waiting for may already be done; conversely, if it might take more than three seconds, you might not be waiting long enough. So if there's something you can check to see whether it's done or not, you can poll, like this:

请注意,您用于触发它的事件,即正文的负载,已经在页面加载周期中发生了,所以无论您在等待什么,都可能已经完成;相反,如果可能需要超过三秒钟,您可能不会等待足够长的时间。所以,如果有什么东西你可以查看它是否已经完成,你可以像这样进行民意调查:

function Func1Delay() {

    check();

    function check() {
        if (theWorkIsDone) {
            // Do something with the work
        }
        else {
            // Check back in 100ms (1/10th of a second)
            setTimeout(check, 100);
        }
    }
}

#3


1  

You want the function to execute as soon as possible, but in every case after your xml has been successfully generated.

您希望函数尽快执行,但在每次成功生成xml之后都要执行。

In this case you should prevent using a fixed amount of time (because you don't know the value exactly), but try the following:

在这种情况下,您应该防止使用固定的时间(因为您不完全知道该值),但请尝试以下操作:

function load(){

    if (/*check here if the xml has *not yet* been generated*/){
          setTimeout(load,50); // try again in 50 milliseconds
          return;
    }

    // do your stuff here
}

This loops as long as your xml is not ready, and kicks in as soon as it's available.

只要你的xml没有准备好,它就会循环,并在它可用时立即启动。

General about setTimeout:

关于setTimeout的一般信息:

You can pass a string, but this is highly discouraged from for several reasons. Instead pass a function reference or a function like this:

你可以传递一个字符串,但出于几个原因,这是非常不鼓励的。而是传递一个函数引用或这样的函数:

// function reference
setTimeout(load,3000) // no `()` !

// function
setTimeout( function(){load()},3000)

If you need paramters be passed to the function, you can't use the first option but need to use the second one, where you can easily pass them load(params).

如果您需要将参数传递给函数,则不能使用第一个选项,但需要使用第二个选项,您可以轻松地将它们传递给load(params)。

If you pass a function like this: setTimeout(load(),3000) it executes the function load and passes its return value to the timeout. You however want the function invoked after 3 seconds and thus only pass the reference to the function.

如果你传递这样的函数:setTimeout(load(),3000)它执行函数load并将其返回值传递给超时。但是,您希望在3秒后调用该函数,从而仅将引用传递给函数。

Notice however, that you have a different scope if you execute the functions this way.

但请注意,如果以这种方式执行函数,则具有不同的范围。