473,403 Members | 2,366 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,403 software developers and data experts.

Help with setTimeout...

I want to use a setTimeout to just loop for a number of seconds, then
fall through to the next bit of code...can this be done by using some
kind of null in the statement, or do I have to assign the code a name
and call it from the setTimeout once it's done?

Thanks so much!

Jul 23 '05 #1
4 2910

"Mool" <Th*********@TheVillage.Net> wrote in message
news:9b********************************@4ax.com...
I want to use a setTimeout to just loop for a number of seconds, then
fall through to the next bit of code...can this be done by using some
kind of null in the statement, or do I have to assign the code a name
and call it from the setTimeout once it's done?
You could set a global variable to 'False' and have the function assigned to
setTimeout change it to 'True', then in your loop check for the 'True' condition
and break..
Thanks so much!

Jul 23 '05 #2
Lee
Mool said:

I want to use a setTimeout to just loop for a number of seconds, then
fall through to the next bit of code...can this be done by using some
kind of null in the statement, or do I have to assign the code a name
and call it from the setTimeout once it's done?


That's not very clear, but it sounds as if you misunderstand
what setTimeout() does. It doesn't loop. It doesn't cause
execution to pause. It simply schedules a task to be performed
after a delay. If you use it to schedule some kind of null,
then that some kind of null will be scheduled to happen after a
delay, but in the meantime, the next bit of code will already be
running.

In short, you do need to write the code as a function and schedule
it using setTimeout().

Jul 23 '05 #3
JRS: In article <9b********************************@4ax.com>, seen in
news:comp.lang.javascript, Mool <Th*********@TheVillage.Net> posted at
Sat, 10 Jul 2004 07:24:22 :
I want to use a setTimeout to just loop for a number of seconds, then
fall through to the next bit of code...can this be done by using some
kind of null in the statement, or do I have to assign the code a name
and call it from the setTimeout once it's done?


You are asking for setTimeout to cause a delay in the current line of
execution (you don't give a location, so I suppose we can rely on your
words meaning what you intend). That was OK in single-process systems
such as ran in plain DOS, but not appropriate in typical GUI
environments.

You should arrange for the initial line of execution to call
setTimeout() and then to terminate naturally. The later code is
provided as the first parameter of setTimeout(); AIUI it can be
anything, but it should be a short string. In this case, a function
call invoking the rest of the code is appropriate.

Basically, a total redesign that changes very little.

This is about the shortest possible example ( -88 in USA, -887 in EU ? )

<input type=button onClick="setTimeout('alert(999)', 1000)">

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
Lee wrote:
Mool said:

I want to use a setTimeout to just loop for a number of seconds, then
fall through to the next bit of code...can this be done by using some
kind of null in the statement, or do I have to assign the code a name
and call it from the setTimeout once it's done?


That's not very clear, but it sounds as if you misunderstand
what setTimeout() does. It doesn't loop. It doesn't cause
execution to pause. It simply schedules a task to be performed
after a delay. If you use it to schedule some kind of null,
then that some kind of null will be scheduled to happen after a
delay, but in the meantime, the next bit of code will already be
running.

In short, you do need to write the code as a function and schedule
it using setTimeout().


setTimeout() can be used as a hack to introduce a delay into Javascript
code:

function first() {
// do stuff
var t = setTimeout(second, 3000);
}
function second() {
// do more stuff
var t = setTimeout(third, 5000);
}
function third() {
// do yet more stuff
}

but anytime you are doing this, it is almost certainly the wrong way to
handle whatever you are doing. Javascript is an event driven environment,
waiting for a set period of time for something to happen (or not happen)
is almost always the wrong way to solve whatever problem you think you're
having.

Provide more details of why you think you need to do this, I'm sure a
solution can be provided that doesn't require your users to wait 3 or 5
seconds for something to happen.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5

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

Similar topics

3
by: Dennis M. Marks | last post by:
There is no problem with the following function. I want to insert code at the point that the function does not repeat. I was pretty sure that it could be inserted at the /// but that repeats with...
14
by: Brandon Hoppe | last post by:
I'm trying to change the src of an ilayer in the parent document from a link inside the ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is for Netscape 4 only....
4
by: Ken | last post by:
Hello I am trying to change the color of a font in a text box. I have been reading about and trying various examples found it this group, but still can't get it right. Here is where I am...
3
by: P Wolpert | last post by:
This is my first post. I hope I don't sound stupid. I have a script conflict when I put two scripts on one page. Both scripts will work if I use one at a time but the menu button script will not...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
2
by: jasonchan | last post by:
I am trying to have a function start with a timer and then switch to another function. However, it is not working at all. Anyone know whats goin on? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.