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

Discussion in 'Javascript' started by gnewsgroup, Dec 28, 2007.

  1. gnewsgroup

    gnewsgroup Guest

    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.
    gnewsgroup, Dec 28, 2007
    #1
    1. Advertising

  2. gnewsgroup wrote:
    > 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);

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 28, 2007
    #2
    1. Advertising

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


    Only two things are infinite, the universe and human stupidity,
    and I'm not sure about the former. - Albert Einstein
    My Pet Programmer, Dec 28, 2007
    #3
  4. gnewsgroup

    Lee Guest

    My Pet Programmer said:
    >
    >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);
    >}


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


    --
    Lee, Dec 28, 2007
    #4
  5. Lee said:
    > My Pet Programmer said:
    >> 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);
    >> }

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


    Only two things are infinite, the universe and human stupidity,
    and I'm not sure about the former. - Albert Einstein
    My Pet Programmer, Dec 28, 2007
    #5
  6. gnewsgroup

    Doug Gunnoe Guest

    On Dec 28, 10:06 am, gnewsgroup <> wrote:
    > 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.
    Doug Gunnoe, Dec 28, 2007
    #6
  7. gnewsgroup

    Lee Guest

    My Pet Programmer said:
    >
    >Lee said:
    >> My Pet Programmer said:
    >>> 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);
    >>> }

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


    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.


    --
    Lee, Dec 28, 2007
    #7
  8. Lee said:
    > My Pet Programmer said:
    >> Lee said:
    >>> My Pet Programmer said:
    >>>> 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);
    >>>> }
    >>> 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.

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


    Only two things are infinite, the universe and human stupidity,
    and I'm not sure about the former. - Albert Einstein
    My Pet Programmer, Dec 28, 2007
    #8
  9. gnewsgroup

    Lee Guest

    My Pet Programmer said:
    >
    >Lee said:
    >> My Pet Programmer said:
    >>> Lee said:
    >>>> My Pet Programmer said:
    >>>>> 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);
    >>>>> }
    >>>> 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.

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


    And I just laughed out loud.


    --
    Lee, Dec 28, 2007
    #9
  10. In comp.lang.javascript message <fl3778$eob$>
    , Fri, 28 Dec 2007 11:11:16, My Pet Programmer
    <> posted:

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

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr J R Stockton, Dec 29, 2007
    #10
  11. -Lost wrote:
    > Response to Lee <>:
    >>> 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.

    >
    > 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
    --
    "Use any version of Microsoft Frontpage to create your site. (This won't
    prevent people from viewing your source, but no one will want to steal it.)"
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
    Thomas 'PointedEars' Lahn, Dec 30, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. evolve
    Replies:
    2
    Views:
    482
    William Ryan
    Nov 2, 2003
  2. A. Kong
    Replies:
    2
    Views:
    4,693
    Iwo Mergler
    Oct 11, 2004
  3. Hylander
    Replies:
    0
    Views:
    396
    Hylander
    Jan 16, 2004
  4. Waldy
    Replies:
    1
    Views:
    622
    Alvin Bruney [MVP]
    Oct 21, 2007
  5. mark4asp
    Replies:
    2
    Views:
    347
    George Ter-Saakov
    Jan 8, 2008
Loading...

Share This Page