Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. javascript delay

javascript delay

Scheduled Pinned Locked Moved JavaScript
javascripthelpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    benams
    wrote on last edited by
    #1

    I use the following code in order to display a fake sending method that lasts 1 second:

    $("#" + action + "-submit-message").text("sending...");

    var startTime = new Date().getTime();
    while (new Date().getTime() < startTime + 1000);

    $("#" + action + "-submit-message").text("sent!");

    Despite of that, I can see only the "sent!" message in the end of the function, when I expect to see the "sending..." for a second. What can be the problem?

    G 1 Reply Last reply
    0
    • B benams

      I use the following code in order to display a fake sending method that lasts 1 second:

      $("#" + action + "-submit-message").text("sending...");

      var startTime = new Date().getTime();
      while (new Date().getTime() < startTime + 1000);

      $("#" + action + "-submit-message").text("sent!");

      Despite of that, I can see only the "sent!" message in the end of the function, when I expect to see the "sending..." for a second. What can be the problem?

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      I guess your while loop is locking out the DOM update while it runs. Try using a timer instead:

      $("#" + action + "-submit-message").text("sending...");

      setTimeout(function() {
      $("#" + action + "-submit-message").text("sent!");
      },1000);

      B 1 Reply Last reply
      0
      • G Graham Breach

        I guess your while loop is locking out the DOM update while it runs. Try using a timer instead:

        $("#" + action + "-submit-message").text("sending...");

        setTimeout(function() {
        $("#" + action + "-submit-message").text("sent!");
        },1000);

        B Offline
        B Offline
        benams
        wrote on last edited by
        #3

        I have tried something similar to:

        function f(){
        $("#" + action + "-submit-message").text("sending...");

        setTimeout(done(),1000);
        }
        function done()
        {
        $("#" + action + "-submit-message").text("sent!");
        }

        but what I get is the "sending..." message permanently(the "sent!" is never displayed)

        G M 2 Replies Last reply
        0
        • B benams

          I have tried something similar to:

          function f(){
          $("#" + action + "-submit-message").text("sending...");

          setTimeout(done(),1000);
          }
          function done()
          {
          $("#" + action + "-submit-message").text("sent!");
          }

          but what I get is the "sending..." message permanently(the "sent!" is never displayed)

          G Offline
          G Offline
          Graham Breach
          wrote on last edited by
          #4

          Try removing the () from inside the setTimeout call, like this:

          setTimeout(done,1000);

          This means you pass the done function to setTimeout to call when the timer runs down, whereas setTimeout(done(),1000); calls done at the start and passes the result to setTimeout to run later, which is not what you want.

          B 1 Reply Last reply
          0
          • G Graham Breach

            Try removing the () from inside the setTimeout call, like this:

            setTimeout(done,1000);

            This means you pass the done function to setTimeout to call when the timer runs down, whereas setTimeout(done(),1000); calls done at the start and passes the result to setTimeout to run later, which is not what you want.

            B Offline
            B Offline
            benams
            wrote on last edited by
            #5

            I think it works, but what if I have to pass parameters to a function? Is there a way to do it?

            G 1 Reply Last reply
            0
            • B benams

              I think it works, but what if I have to pass parameters to a function? Is there a way to do it?

              G Offline
              G Offline
              Graham Breach
              wrote on last edited by
              #6

              You should be able to pass parameters to your functions - but you might like to read up on Javascript closures first, or you could find things don't behave quite the way you expect: http://www.javascriptkit.com/javatutors/closures.shtml[^]

              1 Reply Last reply
              0
              • B benams

                I have tried something similar to:

                function f(){
                $("#" + action + "-submit-message").text("sending...");

                setTimeout(done(),1000);
                }
                function done()
                {
                $("#" + action + "-submit-message").text("sent!");
                }

                but what I get is the "sending..." message permanently(the "sent!" is never displayed)

                M Offline
                M Offline
                Morgs Morgan
                wrote on last edited by
                #7

                Hi, Just inclose the done function in either double or single qouts like so:

                setTimeout('done()', 1000);

                that way you can even pass parameters in the call to the "done" function. Good luck, Morgs

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups