Is it possible to delay document.write("blah blah ..")?

G

gnewsgroup

On page load, is it possible to write out something to the browser
using

document.write("blah blah ...");

And then pause for 2 seconds and write out something else like

document.write("something funny...");

Javascript does not seem to have a sleep function. I've tried

setTimeOut("myFunction()", mydelay);

It does not work.

Any idea? Thanks.
 
M

Martin Honnen

gnewsgroup said:
On page load, is it possible to write out something to the browser
using

document.write("blah blah ...");

And then pause for 2 seconds and write out something else like

document.write("something funny...");

document.write after page load overwrites the the document. And blocking
the page load for two seconds does not sound like a good idea. But you
can easily create and add contents after page load e.g.
var p = document.createElement('p');
p.appendChild(document.createTextNode("something funny..."));
document.body.appendChild(p);
 
M

My Pet Programmer

gnewsgroup said:
On page load, is it possible to write out something to the browser
using

document.write("blah blah ...");

And then pause for 2 seconds and write out something else like

document.write("something funny...");

Javascript does not seem to have a sleep function. I've tried

setTimeOut("myFunction()", mydelay);

It does not work.

Any idea? Thanks.
setTimeout works perfectly. Just because it doesn't do what you want it
to does not mean it does not work.

That said, I have a hack I stopped using a while ago because I decided I
didn't want to pause things for any good reason, and here you go:

function pause(millis) {
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}

~A!


--
Anthony Levensalor
(e-mail address removed)

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
 
L

Lee

My Pet Programmer said:
gnewsgroup said:
setTimeout works perfectly. Just because it doesn't do what you want it
to does not mean it does not work.

That said, I have a hack I stopped using a while ago because I decided I
didn't want to pause things for any good reason, and here you go:

function pause(millis) {
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}

Please don't post such horrors to this newsgroup.
You should NEVER use code like that in a web page.


--
 
M

My Pet Programmer

Lee said:
My Pet Programmer said:

Please don't post such horrors to this newsgroup.
You should NEVER use code like that in a web page.
I tend to agree, and I wouldn't. Haven't used that crud in so long I
can't even remember. But he wanted it, so I gave it to him.

~A!

--
Anthony Levensalor
(e-mail address removed)

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
 
D

Doug Gunnoe

On page load, is it possible to write out something to the browser
using

document.write("blah blah ...");

And then pause for 2 seconds and write out something else like

document.write("something funny...");

Javascript does not seem to have a sleep function.  I've tried

setTimeOut("myFunction()", mydelay);

It does not work.

Any idea?  Thanks.

To find out why that does not work, after the document.write, right
click and choose view source.

I'm not sure what you are trying to do, but maybe look into modifying
innerHTML instead of doing document.write.
 
L

Lee

My Pet Programmer said:
Lee said:
I tend to agree, and I wouldn't. Haven't used that crud in so long I
can't even remember. But he wanted it, so I gave it to him.

I understand, but giving people what they ask for isn't always
doing them a service. There's also the risk that other people
will find it via a search and think it's a recommended solution.


--
 
M

My Pet Programmer

Lee said:
My Pet Programmer said:

I understand, but giving people what they ask for isn't always
doing them a service. There's also the risk that other people
will find it via a search and think it's a recommended solution.
Good point. I apologize to all future readers of this thread in advance.
Don't use that code, I used it years ago when I was also using
document.all, and I just threw up in my mouth a little bit.

~A!

--
Anthony Levensalor
(e-mail address removed)

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
 
L

Lee

My Pet Programmer said:
Lee said:
Good point. I apologize to all future readers of this thread in advance.
Don't use that code, I used it years ago when I was also using
document.all, and I just threw up in my mouth a little bit.

And I just laughed out loud.


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Fri, 28 Dec 2007 11:11:16, My Pet Programmer
That said, I have a hack I stopped using a while ago because I decided
I didn't want to pause things for any good reason, and here you go:

function pause(millis) {
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}

Bloated code for an inappropriate algorithm. Consider unbloated :

function pause(millis) {
var stop = +new Date() + millis ;
while (+new Date() < stop) {} ;
}
 
T

Thomas 'PointedEars' Lahn

-Lost said:
Yeah, that was definitely good. You're making a great impression,
Anthony.

Definitely a true underdog story. ; )

He apologized already. There is no need to beat a dead horse.

I think another apology would be in order now.


PointedEars
 

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
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top