JavaScript Wait or Pause

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006

Anyone know how to do a wait or a pause in JavaScript. Here's what I'm trying to do:

  var head = document.getElementsByTagName("head")[0];
  var script = document.createElement("script");
  script.src = some.url;
  head.appendChild(script);
 
  // wait for script file to be loaded
  // continue on in this script

The wait part is where I'm struggling. What is the best way to wait until the readyState says the file is loaded?

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 10/22/2008
trigger from the loaded script?

Maybe use the loaded script to trigger the continuation? So like, put part 2 of the script in a function, and have the loaded script call that function? But there's probably a more formal, standard way of doing what you're asking.

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
Won't Work

I need a general loader. For example, The loader might load an external file like jquery.js and then later in the script it will use jquery.js. For my use case to workthe external script can't fire off a function in the local script. And, the local script can't run until the external file is loaded (a dependency).

The use case, if that makes it easier is drupals javascript preprocessor. The preprocessor takes multiple js files and turns them into one js file which improves website performance.

This works great for js files that are part of drupal, a theme, or an add on module. But, what if we want to use a file from the google js libraries or somewhere else? Say, replace jquery.js with one provided by google. Now, let's keep jquery.js in the dependency flow of the rest of the preprocessed files.

To do this we need to add jquery.js in the flow of the one file and wait for it to be loaded before we finish processing the rest of the js in that file.

Thoughts? Does this make sense?

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 09/25/2008
JS falls short.

I've done a bit of searching and found no real delay functionality and only two ways to fake a sleep/delay in JS.

One is what I call a dead loop that destroys the CPU by basically counting to a bazillion, checking the time, and coming back for more. The other is to use a modal dialog box with timeout to waste time without wasting CPU cycles in the delay.

I doubt either is what you want. I definitely call this a shortcoming of JS.
Jim

Joined: 11/28/2008
Kludge alert

It'd be messy, but you could do it with two separate functions and AJAX. Have the first function start the process, load the script through AJAX, and call the second function upon the successful execution of the AJAX. It'd be quite nasty looking, but it'd work, at least, theoretically.

--- Mr. DOS

Joined: 11/28/2008
We use an AJAX approach

In the product we create at my day job, we have an AJAX version we call ScriptImport. We use synchronous connections to cause blocking until the response comes back.

We primarily use this when other AJAX responses which contain HTML have script blocks which need to be added into the DOM. We parse the response HTML for external scripts and script blocks.

For the inline blocks we just build a script element and set its innerHTML. For externals we use AJAX to get the remote file, then build a script element and set the response text as the element's innerHTML.

I wish I could show it to you, but it's proprietary. It works very very well, though. Quite reliable.

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
Wouldn't Work Either

@JAAulde Thanks for your thoughts but that won't work either. There are limitations with ajax callbacks on the domains they can call back to. These limitations make those methods non-starters.

I think I'm going to have to try another approach. I have some ideas brewing.

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 11/28/2008
Yeah, domain constraints kill

Yeah, domain constraints kill this if you're not getting code from your domain. All of our code is coming from our own domain.

Joined: 11/28/2008
script.onload ?

Matt,

Images have an onload, do script elements?

therealdealsince1982
therealdealsince1982's picture
javascript sleep

I have searched/googled quite a few webpages on javascript sleep/wait... and there is NO answer if you want javascript to "RUN, DELAY, RUN"... what most people got was either, "RUN, RUN(useless stuff), RUN" or "RUN, RUN + delayed RUN"....

So I ate some burgers and got thinking:::
here is a solution that works... but you have to chop up your running codes...:::

replace <.. with < to run..
//.........................................
//example1:

<..html>
<..body>
<..div id="id1">DISPLAY<../div>

<..script>
//javascript sleep by "therealdealsince1982"; copyrighted 2009
//setInterval
var i = 0;

function run() {
//pieces of codes to run
if (i==0){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" is ran<../p>"; }
if (i==1){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" is ran<../p>"; }
if (i==2){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" is ran<../p>"; }
if (i >2){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" is ran<../p>"; }
if (i==5){document.getElementById("id1").innerHTML= "<..p>all code segment finished running<../p>"; clearInterval(t); } //end interval, stops run
i++; //segment of code finished running, next...
}

t=setInterval("run()",1000);

<../script>
<../body>
<../html>

//....................................
//example2:

<..html>
<..body>
<..div id="id1">DISPLAY<../div>

<..script>
//javascript sleep by "therealdealsince1982"; copyrighted 2009
//setTimeout
var i = 0;

function run() {
//pieces of codes to run, can use switch statement
if (i==0){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" ran<../p>"; sleep(1000);}
if (i==1){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" ran<../p>"; sleep(2000);}
if (i==2){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" ran<../p>"; sleep(3000);}
if (i==3){document.getElementById("id1").innerHTML= "<..p>code segment "+ i +" ran<../p>";} //stops automatically
i++;
}

function sleep(dur) {t=setTimeout("run()",dur);} //starts flow control again after dur

run(); //starts flow
<../script>
<../body>
<../html>

Joined: 03/31/2011
I think the neatest way to do

I think the neatest way to do this is:

function part1() {
    ...
    object.doSomething();
    ...
    object.onLoadOrWhatever(part2);
}

function part2() {
    ...
    someObject.onLoadOrWhatever(part3);
}

function part3() {
   etc.
}

// start the whole thing:
part1();

Joined: 11/28/2008
LABjs

There are now a plethora of script loaders in the market. I recently spent several weeks bulk converting a VERY big project to load it's js via LABjs.