473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

javascript wait

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

Oct 25 '06 #1
14 21698
Rik
st************@gmail.com wrote:
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...?
Don't do that.
Use setTimeout();

In this case:
setTimeout("iDontWaitForNobody()",5000);
--
Rik Wasmus
Oct 25 '06 #2

st************@gmail.com wrote:
I've gathered that javascript doesn't have a handy wait() function,
I think you mightu be looking for JavaScript's setTimeout()

Peter

Oct 25 '06 #3

Rik wrote:
st************@gmail.com wrote:
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...?

Don't do that.
Use setTimeout();

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

setTimeout(iDontWaitForNobody, 5000);

Oct 25 '06 #4
Peter Michaux said the following on 10/24/2006 11:03 PM:
Rik wrote:
>st************@gmail.com wrote:
>>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...?
Don't do that.
Use setTimeout();

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

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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 25 '06 #5
Rik
Peter Michaux wrote:
Rik wrote:
>st************@gmail.com wrote:
>>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...?

Don't do that.
Use setTimeout();

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

or even just

setTimeout(iDontWaitForNobody, 5000);
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 way it's easier to backtrace everything if there's an error.

Wether this is good practise or not is something I don't know, I usually
don't do that much javascript :)

--
Rik Wasmus
Oct 25 '06 #6
Rik
Randy Webb wrote:
Peter Michaux said the following on 10/24/2006 11:03 PM:
>Rik wrote:
>>st************@gmail.com wrote:
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...?
Don't do that.
Use setTimeout();

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

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.
Hmmmz, you've got a point.
If it's a simple halt in a function, you could call a second function that
takes over like this. If one wants to stop all code, that would require all
events fired be focusing/moving/etc. will have to stop.

I really don't know enough js to even start to consider how that second
option could be implemented...
--
Rik Wasmus
Oct 25 '06 #7
Randy Webb wrote:
Peter Michaux said the following on 10/24/2006 11:03 PM:
Rik wrote:
st************@gmail.com wrote:
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...?
Don't do that.
Use setTimeout();

In this case:
setTimeout("iDontWaitForNobody()",5000);
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.
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

Oct 25 '06 #8
Peter Michaux said the following on 10/24/2006 11:41 PM:
Randy Webb wrote:
>Peter Michaux said the following on 10/24/2006 11:03 PM:
>>Rik wrote:
st************@gmail.com wrote:
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...?
Don't do that.
Use setTimeout();

In this case:
setTimeout("iDontWaitForNobody()",5000);
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.

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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 25 '06 #9
Rik said the following on 10/24/2006 11:36 PM:
Randy Webb wrote:
>Peter Michaux said the following on 10/24/2006 11:03 PM:
>>Rik wrote:
st************@gmail.com wrote:
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...?
Don't do that.
Use setTimeout();

In this case:
setTimeout("iDontWaitForNobody()",5000);
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.

Hmmmz, you've got a point.
If it's a simple halt in a function, you could call a second function that
takes over like this. If one wants to stop all code, that would require all
events fired be focusing/moving/etc. will have to stop.
Not really. If you had a true wait(), then all execution *will* stop
because that is what wait() does. After the sleep is finished, its a rat
race as to what events will still fire and in what order, but they
should fire in the order they were initiated.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 25 '06 #10
Hi,

Rik wrote:
Peter Michaux wrote:
>or even just

setTimeout(iDontWaitForNobody, 5000);

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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 25 '06 #11
Hi,

st************@gmail.com wrote:
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 25 '06 #12
Rik
Laurent Bugnion wrote:
(which is an object, because in JavaScript, functions are objects).
Check, you're right.
--
Rik Wasmus
Oct 25 '06 #13


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...

*** Sent via Developersdex http://www.developersdex.com ***
Mar 6 '07 #14
Hi,

Peter Tran wrote:
>
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
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Mar 10 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: m miller | last post by:
I have the following javascript in my html: <script language="javascript"> function setfocus() { if (document.body.style.cursor="default") { document.body.style.cursor="wait";...
2
by: Chris | last post by:
Hi, I have a Javascript function that loads a page with a progress bar for long process. The progress bar is a gif animation and for some reason it the animation is stuck when the function is...
8
by: C.Joseph Drayton | last post by:
Hi All, I am calling a PHP script that does an operation then sends back a result. I want JavaScript to wait until that result has been recieved. I am using the following code. What can I do to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.