Send variable to setTimeout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    Send variable to setTimeout

    Howdy All!

    How can I make something like this work?

    var message = "This will appear in 10 seconds";

    setTimeout ('alert(message )', 10000);

    Thanks for any advice!

    Rob
    :)
    :>
    :]
  • McKirahan

    #2
    Re: Send variable to setTimeout

    "Robert Mark Bram" <relaxedrob@rem ove.this.optusn et.com.au> wrote in message
    news:opr00xp9e0 yneqvgrmb@news. optusnet.com.au ...[color=blue]
    > Howdy All!
    >
    > How can I make something like this work?
    >
    > var message = "This will appear in 10 seconds";
    >
    > setTimeout ('alert(message )', 10000);
    >
    > Thanks for any advice!
    >
    > Rob
    > :)
    > :>
    > :][/color]

    You've got it; however, the message should reflect that it appeared "after"
    ten seconds not "in" ten seconds. Try the following as-is; watch for
    word-wrap.


    <html>
    <head>
    <title>timeout. htm</title>
    <script language="javas cript" type="text/javascript">
    <!--
    var message = "This will appear after 10 seconds";
    setTimeout ('alert(message )', 10000);
    // -->
    </script>
    </head>
    <body>
    </body>
    </html>


    Comment

    • McKirahan

      #3
      Re: Send variable to setTimeout

      Better yet:

      var message = "This page loaded 10 seconds ago.";


      Comment

      • Janwillem Borleffs

        #4
        Re: Send variable to setTimeout

        Robert Mark Bram wrote:[color=blue]
        > How can I make something like this work?
        >
        > var message = "This will appear in 10 seconds";
        >
        > setTimeout ('alert(message )', 10000);
        >[/color]

        If you want to use this in a function, consider the following:

        function alertmsg(messag e) {
        alert(message);
        // Escape single quotes in message
        message = message.replace (/'/g,"\\'");
        setTimeout ("alertmsg(' " + message + "')", 10000);
        }

        Otherwise, follow McKirahan's advise...


        JW



        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Send variable to setTimeout

          "Janwillem Borleffs" <jw@jwscripts.c om> writes:
          [color=blue]
          > Robert Mark Bram wrote:[color=green]
          >> How can I make something like this work?
          >>
          >> var message = "This will appear in 10 seconds";
          >>
          >> setTimeout ('alert(message )', 10000);[/color][/color]
          [color=blue]
          > If you want to use this in a function, consider the following:
          >
          > function alertmsg(messag e) {
          > alert(message);
          > // Escape single quotes in message
          > message = message.replace (/'/g,"\\'");
          > setTimeout ("alertmsg(' " + message + "')", 10000);
          > }[/color]

          That fails in several ways for
          alertmsg("I'm happy because my backslash (\\) is slanted!\nYes I am");
          You handle the ', but the "\" is interpreted as an escape and is lost
          (because '\)' becomes ')', and the newline is also used literally,
          giving a syntax error, since newlines are not allowed inside string
          literals..
          [color=blue]
          > Otherwise, follow McKirahan's advise...[/color]

          That will work, but requires a global variable.

          There is one other way, that sadly only works in IE from version 5.5:
          using a function as argument to setTimeout:
          ---
          function alertmsg(messag e) {
          alert(message);
          // Escape single quotes in message
          setTimeout (function(){ale rtmsg(message); }, 10000);
          }
          ---
          This constant recalling, which was not in the original posters code,
          is better handled with setInterval anyway:
          ---
          function startalerts(mes sage) {
          return setInterval(fun ction(){alert(m essage);},10000 );
          }
          ---

          The equivalent for the original poster's problem is:

          var message = "whatever";
          setTimeout(func tion(){alert(me ssage);},10000) ;


          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Janwillem Borleffs

            #6
            Re: Send variable to setTimeout

            Lasse Reichstein Nielsen wrote:[color=blue]
            > That fails in several ways for
            > alertmsg("I'm happy because my backslash (\\) is slanted!\nYes I
            > am"); You handle the ', but the "\" is interpreted as an escape and
            > is lost (because '\)' becomes ')', and the newline is also used
            > literally, giving a syntax error, since newlines are not allowed
            > inside string literals..
            >[/color]

            Well, that's easy enough to fix:

            function alertmsg(messag e) {
            message = unescape(messag e);
            alert(message);
            setTimeout ("alertmsg(' " + escape(message) + "')", 10000);
            }


            JW



            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Send variable to setTimeout

              Lasse Reichstein Nielsen wrote:
              [color=blue]
              > There is one other way, that sadly only works in IE from version 5.5:
              > using a function as argument to setTimeout:[/color]

              Mozilla/4.0+ supports that as well, it even supports passing non-string
              arguments to the called function, passing them as third, fourth aso.
              argument of window.setTimeo ut(...).




              PointedEars

              Comment

              Working...