Get value returned by function called in setTimeout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ScripterSam
    New Member
    • Oct 2007
    • 16

    #1

    Get value returned by function called in setTimeout

    Hello Freinds,
    I have a funtion to call from setTimeout function which will return some value..
    I mean I need to get the value, the function called from the setTimeout function returns...
    Please help
  • rohitchawla
    New Member
    • Jul 2007
    • 85

    #2
    u can try something like this

    [CODE=javascript]
    function a()
    {
    var x="45";
    return x;
    }
    var y;
    setTimeout(y=a( ),1000);
    alert(y);
    [/CODE]

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by rohitchawla
      u can try something like this

      [CODE=javascript]
      function a()
      {
      var x="45";
      return x;
      }
      var y;
      setTimeout(y=a( ),1000);
      alert(y);
      [/CODE]

      It will be ...

      [code=javascript]
      setTimeout("y=a ()",1000);
      [/code]

      Debasis Jana

      Comment

      • ScripterSam
        New Member
        • Oct 2007
        • 16

        #4
        Originally posted by rohitchawla
        u can try something like this

        [CODE=javascript]
        function a()
        {
        var x="45";
        return x;
        }
        var y;
        setTimeout(y=a( ),1000);
        alert(y);
        [/CODE]

        I tried this out but the alert message is showing undefined for y value...

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          Originally posted by ScripterSam
          I tried this out but the alert message is showing undefined for y value...
          hi ...

          of course i comment the code ... have a look at it:

          [CODE=javascript]
          // this function returns '45'
          function a() {
          var x = "45";
          return x;
          }
          // declare global var y
          var y;

          // after 1 second y will be set to '45'
          setTimeout('y = a();', 1000);

          // the alert is called instantly but y is not set yet!
          // so you get 'undefined'
          alert(y);
          [/CODE]

          to fix that you may use:

          [CODE=javascript]
          function a() {
          var x = "45";
          return x;
          }

          var y;

          function set_y () {
          y = a();
          alert(y);
          }

          setTimeout(set_ y, 1000);
          [/CODE]
          kind regards

          ps: in case you use:

          [CODE=javascript]setTimeout(y = a(), 1000);[/CODE]
          the timeout is useless ... y is assigned instantly the '45' from function a ... nothing more

          Comment

          • rohitchawla
            New Member
            • Jul 2007
            • 85

            #6
            well yes but u can assign any function associated with it
            it was just an example
            and thanks gits for fixing my mistakes
            i am also new to js

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              Originally posted by rohitchawla
              well yes but u can assign any function associated with it
              it was just an example
              and thanks gits for fixing my mistakes
              i am also new to js
              heya rohitchawla,

              no problem ... let me give a short additional explaination of the setTimeout method:

              usage:

              [CODE=javascript]window.setTimeo ut(funcref, milliseconds);[/CODE]
              funcref has to be a function-reference you may use the function-name here as in my example or the 'function-signature' enclosed in qoutes as in dmjpros example ... because it needs this reference since the code should be executed in the future ... and therefore has not to be called instantly ...

              kind regards

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Changed thread title to better describe the problem.

                Comment

                Working...