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

html - Want to set delay in javascript - Stack Overflow

programmeradmin1浏览0评论

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

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

In this Func1Delay() function i have written code to delay execution of 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??

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

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

In this Func1Delay() function i have written code to delay execution of 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??

Share Improve this question edited Dec 10, 2012 at 9:28 Charles 51.5k13 gold badges106 silver badges144 bronze badges asked Dec 10, 2012 at 9:23 EkkyEkky 7905 gold badges14 silver badges29 bronze badges 4
  • How do you load the XML file? I am sure the loading mechanism provides a callback which fires when the file is loaded, after 1, 2, 10 or whatever seconds. – Salman Arshad Commented Dec 10, 2012 at 9:27
  • downloadUrl("location.xml", function(data) { var xml = data.responseXML;}) this is how i load xml file in javascript load() function..@Salman A – Ekky Commented Dec 10, 2012 at 10:20
  • downloadUrl does provide a callback. This function is retired though. – Salman Arshad Commented Dec 10, 2012 at 10:21
  • then what you suggest me to do @Salman A – Ekky Commented Dec 10, 2012 at 10:29
Add a ment  | 

3 Answers 3

Reset to default 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:

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

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.

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);
        }
    }
}

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

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.

General about 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).

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.

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

发布评论

评论列表(0)

  1. 暂无评论