sleep function with a parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M B HONG 20

    sleep function with a parameter

    Hi all -

    I'm trying to emulate a sleep function in javascript. I know of this
    method for example:

    window.setTimeo ut("function1() ;", 500);

    However, I was wondering if there was a way to call the function with a
    parameter, for example:

    window.setTimeo ut("function1(s tring1);", 500);

    This gives me a javascript error. Any help will be greatly
    appreciated. Thanks in advance.

  • M B HONG 20

    #2
    Re: sleep function with a parameter

    Nevermind, it was a syntax error, it turns out you could just do

    window.setTimeo ut(function1(st ring1), 500)

    you dont need the quotes in the function declaration.

    Comment

    • web.dev

      #3
      Re: sleep function with a parameter


      M B HONG 20 wrote:[color=blue]
      > Hi all -
      >
      > I'm trying to emulate a sleep function in javascript. I know of this
      > method for example:
      >
      > window.setTimeo ut("function1() ;", 500);
      >
      > However, I was wondering if there was a way to call the function with a
      > parameter, for example:
      >
      > window.setTimeo ut("function1(s tring1);", 500);
      >
      > This gives me a javascript error. Any help will be greatly
      > appreciated. Thanks in advance.[/color]

      Does function1 expect a string in its parameter? Then you should be
      sending in a string:

      window.setTimeo ut("function1(' string1');", 500);

      Comment

      • Michael Winter

        #4
        Re: sleep function with a parameter

        On 18/10/2005 00:43, M B HONG 20 wrote:
        [color=blue]
        > Nevermind, it was a syntax error, it turns out you could just do
        >
        > window.setTimeo ut(function1(st ring1), 500)[/color]

        That's something entirely different. Here you're calling the function,
        function1, immediately. The return value from that call will be
        converted to a string and evaluated approximately 500 milliseconds
        later. This can be proven with:

        function myFunction() {
        alert('Called') ;
        return '';
        }

        setTimeout(myFu nction(), 10000);

        No dialog box should appear for around ten seconds, yet it will appear
        immediately.

        I would guess that your identifier, string1, is local to some function.
        As the setTimeout function is evaluated in global scope, an error would
        occur with

        setTimeout('fun ction1(string1) ', 500);

        because when executed, string1 won't exist. To get around that,
        concatenate the value of string1 into the literal, remembering to
        include nested quotes:

        setTimeout('fun ction1("' + string1 + '");', 500);

        If string1 contained the value, 'my string', the code above would call

        function1("my string");

        approximately 500ms later.

        Mike

        --
        Michael Winter
        Prefix subject with [News] before replying by e-mail.

        Comment

        • M B HONG 20

          #5
          Re: sleep function with a parameter

          actually my mistake, the method

          window.setTimeo ut(function1(st ring1), 500)

          does not work, it does not actually sleep for the 500ms before firing
          the function.

          web.dev, thanks for the reply. Your method works for a straight string
          passing through, but I need the function to be called with a variable
          parameter.

          Comment

          • M B HONG 20

            #6
            Re: sleep function with a parameter

            Thanks a lot Michael, that did the trick.

            Comment

            • Robert

              #7
              Re: sleep function with a parameter

              M B HONG 20 wrote:[color=blue]
              > Hi all -
              >
              > I'm trying to emulate a sleep function in javascript. I know of this
              > method for example:
              >
              > window.setTimeo ut("function1() ;", 500);[/color]

              After reading your previous posts in your thread I assume you wanted to
              do something like
              window.setTimeo ut(func, 500);
              where func is a reference to a function, but also want the ability to
              pass a parameter.

              You can accomplish this using closures.
              Example:

              function displayMessage( message)
              {
              alert(message);
              }

              function setDelayedMessa ge()
              {
              var message = "hello";
              var func = function()
              {
              displayMessage( message);
              }
              window.setTimeo ut(func, 500);
              }

              setDelayedMessa ge();

              Comment

              Working...