javascript wait

S

stephen.durkin

I've gathered that javascript doesn't have a handy wait() function, so
I made the following function makeDelay() to make use of PHP's sleep(),
which then does the waiting, and responds with "done"...my problem is
that the javascript doesn't wait for makeDelay(seconds) to finish...?

makeDelay(5);
iDontWaitForNobody();

function makeDelay(seconds) {

url="make_delay.php?delay=" + seconds;
http.open("GET",url,true);

loadStatus=document.getElementById("loadStatus");

http.onreadystatechange=function() {
if (http.readyState==1) {
loadStatus.src="balls/load1.gif";
}
if (http.readyState==2) {
loadStatus.src="balls/load2.gif";
}
if (http.readyState==3) {
loadStatus.src="balls/load3.gif";
}
if (http.readyState==4) {
if(http.responseText == "done")
loadStatus.src="balls/load0.gif";
}
}
http.send(null);
}
 
P

Peter Michaux

Rik said:
Don't do that.
Use setTimeout();

In this case:
setTimeout("iDontWaitForNobody()",5000);

or even just

setTimeout(iDontWaitForNobody, 5000);
 
R

Randy Webb

Peter Michaux said the following on 10/24/2006 11:03 PM:
or even just

setTimeout(iDontWaitForNobody, 5000);

Neither of which does what the OP wanted - duplicating PHP's wait()
function which pauses execution of *all* code until the time is up.
 
P

Peter Michaux

Randy said:
Peter Michaux said the following on 10/24/2006 11:03 PM:

Neither of which does what the OP wanted - duplicating PHP's wait()
function which pauses execution of *all* code until the time is up.

True but since the OP didn't state the goal it could be that setTimeout
does exactly what he wants in this case. But maybe not.

Using the Internet to create the wait seems like a bad way to go since
the timing wouldn't be controllable.

I bet there is a setTimeout solution if the OP knows about it and
thinks about the problem with setTimeout in mind.

Peter
 
R

Randy Webb

Peter Michaux said the following on 10/24/2006 11:41 PM:
True but since the OP didn't state the goal it could be that setTimeout
does exactly what he wants in this case. But maybe not.

Using the Internet to create the wait seems like a bad way to go since
the timing wouldn't be controllable.

It's a bad idea, period, because a true wait() locks up the browser. The
time is very controllable though. And, that is very easy to test.
 
L

Laurent Bugnion

Hi,
Peter Michaux wrote:

True,

I usually follow this procedure though:
If it calls a function directly, quote it.
If it calls a function which name is in a variable, don't quote it
offcourse.

That's not what setTimeout(iDontWaitForNobody, 5000) does. It gives to
the setTimeout function a reference to the iDontWaitForNobody function
(which is an object, because in JavaScript, functions are objects). If
you're familiar with C#, think of it as a delegate (though it's actually
much more direct, and also much older than delegates).

For parameter-less methods, this way is faster, since the statement
doesn't need to be parsed and evaluated.

For methods with parameters, it doesn't work, but then you can use closure.

HTH,
Laurent
 
L

Laurent Bugnion

Hi,

I've gathered that javascript doesn't have a handy wait() function, so
I made the following function makeDelay() to make use of PHP's sleep(),
which then does the waiting, and responds with "done"...my problem is
that the javascript doesn't wait for makeDelay(seconds) to finish...?

makeDelay(5);
iDontWaitForNobody();

function makeDelay(seconds) {

url="make_delay.php?delay=" + seconds;
http.open("GET",url,true);

loadStatus=document.getElementById("loadStatus");

http.onreadystatechange=function() {
if (http.readyState==1) {
loadStatus.src="balls/load1.gif";
}
if (http.readyState==2) {
loadStatus.src="balls/load2.gif";
}
if (http.readyState==3) {
loadStatus.src="balls/load3.gif";
}
if (http.readyState==4) {
if(http.responseText == "done")
loadStatus.src="balls/load0.gif";
}
}
http.send(null);
}

The reason why it doesn't work is that you call the web service method
in an asynchronous way. XmlHttpRequest also supports synchronous calls,
but I really, really recommend against it in most cases. JavaScript is
not multi-threaded, so if you let one thread sleeps, everything sleeps.

Use timers instead, like others recommended.

HTH,
Laurent
 
P

Peter Tran

If you really want to do it the way you are doing it, just change the
following line:

From:
http.open("GET",url,true);

To:
http.open("GET",url,false);

This will make the call syncrhonous...
 
L

Laurent Bugnion [MVP]

Hi,

Peter said:
If you really want to do it the way you are doing it, just change the
following line:

From:
http.open("GET",url,true);

To:
http.open("GET",url,false);

This will make the call syncrhonous...

You should quote what you reply to.

Also, I know how to make a synchronous call. You're replying to the
wrong person. That all makes your post quite difficult to understand.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top