Javascript Wait












1















I am trying to make the script pause for about 1 second, then continue executing the script, but I can't seem to figure out how. Here is my code:






function hello() {
alert("Hi!")
//I need about a 1 seconed pause here;
alert("Hi again!");
}

<button onclick="hello()">Say Hi!</button>





I already tried the following:






function hello() {
alert("Hi!")
setTimeout(myFunction, 3000)//I thought this would wait, run the function, then return to the function hello()...
alert("Hi again!")
}
function myFunction {
a = document.getElementById("blank")
a.innerHTML = "wait complete"
}

<button onclick="hello()">Say Hi!</button>
<div id="blank">
...
</div>












share|improve this question


















  • 2





    setTimeout is asynchronous, so it won't wait to continue running the script. You need to put your second alert in a separate function, then pass that as the callback parameter of your setTimeout call.

    – Caleb H.
    Nov 20 '18 at 23:45











  • For a start, you've got a syntax error, missing empty parentheses after myFunction. Next, to clarify, setTimeout schedules a function call for later but then continues executing the rest of the script. Everything you want to delay needs to be in the function passed to setTimeout, you can't just make a function wait and continue later like that.

    – Jacque Goupil
    Nov 20 '18 at 23:49


















1















I am trying to make the script pause for about 1 second, then continue executing the script, but I can't seem to figure out how. Here is my code:






function hello() {
alert("Hi!")
//I need about a 1 seconed pause here;
alert("Hi again!");
}

<button onclick="hello()">Say Hi!</button>





I already tried the following:






function hello() {
alert("Hi!")
setTimeout(myFunction, 3000)//I thought this would wait, run the function, then return to the function hello()...
alert("Hi again!")
}
function myFunction {
a = document.getElementById("blank")
a.innerHTML = "wait complete"
}

<button onclick="hello()">Say Hi!</button>
<div id="blank">
...
</div>












share|improve this question


















  • 2





    setTimeout is asynchronous, so it won't wait to continue running the script. You need to put your second alert in a separate function, then pass that as the callback parameter of your setTimeout call.

    – Caleb H.
    Nov 20 '18 at 23:45











  • For a start, you've got a syntax error, missing empty parentheses after myFunction. Next, to clarify, setTimeout schedules a function call for later but then continues executing the rest of the script. Everything you want to delay needs to be in the function passed to setTimeout, you can't just make a function wait and continue later like that.

    – Jacque Goupil
    Nov 20 '18 at 23:49
















1












1








1








I am trying to make the script pause for about 1 second, then continue executing the script, but I can't seem to figure out how. Here is my code:






function hello() {
alert("Hi!")
//I need about a 1 seconed pause here;
alert("Hi again!");
}

<button onclick="hello()">Say Hi!</button>





I already tried the following:






function hello() {
alert("Hi!")
setTimeout(myFunction, 3000)//I thought this would wait, run the function, then return to the function hello()...
alert("Hi again!")
}
function myFunction {
a = document.getElementById("blank")
a.innerHTML = "wait complete"
}

<button onclick="hello()">Say Hi!</button>
<div id="blank">
...
</div>












share|improve this question














I am trying to make the script pause for about 1 second, then continue executing the script, but I can't seem to figure out how. Here is my code:






function hello() {
alert("Hi!")
//I need about a 1 seconed pause here;
alert("Hi again!");
}

<button onclick="hello()">Say Hi!</button>





I already tried the following:






function hello() {
alert("Hi!")
setTimeout(myFunction, 3000)//I thought this would wait, run the function, then return to the function hello()...
alert("Hi again!")
}
function myFunction {
a = document.getElementById("blank")
a.innerHTML = "wait complete"
}

<button onclick="hello()">Say Hi!</button>
<div id="blank">
...
</div>








function hello() {
alert("Hi!")
//I need about a 1 seconed pause here;
alert("Hi again!");
}

<button onclick="hello()">Say Hi!</button>





function hello() {
alert("Hi!")
//I need about a 1 seconed pause here;
alert("Hi again!");
}

<button onclick="hello()">Say Hi!</button>





function hello() {
alert("Hi!")
setTimeout(myFunction, 3000)//I thought this would wait, run the function, then return to the function hello()...
alert("Hi again!")
}
function myFunction {
a = document.getElementById("blank")
a.innerHTML = "wait complete"
}

<button onclick="hello()">Say Hi!</button>
<div id="blank">
...
</div>





function hello() {
alert("Hi!")
setTimeout(myFunction, 3000)//I thought this would wait, run the function, then return to the function hello()...
alert("Hi again!")
}
function myFunction {
a = document.getElementById("blank")
a.innerHTML = "wait complete"
}

<button onclick="hello()">Say Hi!</button>
<div id="blank">
...
</div>






javascript html wait






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 23:41









SomeoneSomeone

61




61








  • 2





    setTimeout is asynchronous, so it won't wait to continue running the script. You need to put your second alert in a separate function, then pass that as the callback parameter of your setTimeout call.

    – Caleb H.
    Nov 20 '18 at 23:45











  • For a start, you've got a syntax error, missing empty parentheses after myFunction. Next, to clarify, setTimeout schedules a function call for later but then continues executing the rest of the script. Everything you want to delay needs to be in the function passed to setTimeout, you can't just make a function wait and continue later like that.

    – Jacque Goupil
    Nov 20 '18 at 23:49
















  • 2





    setTimeout is asynchronous, so it won't wait to continue running the script. You need to put your second alert in a separate function, then pass that as the callback parameter of your setTimeout call.

    – Caleb H.
    Nov 20 '18 at 23:45











  • For a start, you've got a syntax error, missing empty parentheses after myFunction. Next, to clarify, setTimeout schedules a function call for later but then continues executing the rest of the script. Everything you want to delay needs to be in the function passed to setTimeout, you can't just make a function wait and continue later like that.

    – Jacque Goupil
    Nov 20 '18 at 23:49










2




2





setTimeout is asynchronous, so it won't wait to continue running the script. You need to put your second alert in a separate function, then pass that as the callback parameter of your setTimeout call.

– Caleb H.
Nov 20 '18 at 23:45





setTimeout is asynchronous, so it won't wait to continue running the script. You need to put your second alert in a separate function, then pass that as the callback parameter of your setTimeout call.

– Caleb H.
Nov 20 '18 at 23:45













For a start, you've got a syntax error, missing empty parentheses after myFunction. Next, to clarify, setTimeout schedules a function call for later but then continues executing the rest of the script. Everything you want to delay needs to be in the function passed to setTimeout, you can't just make a function wait and continue later like that.

– Jacque Goupil
Nov 20 '18 at 23:49







For a start, you've got a syntax error, missing empty parentheses after myFunction. Next, to clarify, setTimeout schedules a function call for later but then continues executing the rest of the script. Everything you want to delay needs to be in the function passed to setTimeout, you can't just make a function wait and continue later like that.

– Jacque Goupil
Nov 20 '18 at 23:49














2 Answers
2






active

oldest

votes


















2














You were on the right track with the setTimeout function:






function hello() {
alert("Hi!")
setTimeout(function() {
alert("Hi again!");
}, 1000)

}

<button onclick="hello()">Say Hi!</button>








share|improve this answer































    1














    An idiom that's becoming common as async/await is available in browsers it to make an async function and await a pause() function the returns a promise:






    let pause = (time) => new Promise(resolve => setTimeout(resolve, time))

    async function hello() {
    console.log("Hi!")
    await pause(2000)
    console.log("Hi again!");
    }

    <button onclick="hello()">Say Hi!</button>








    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403262%2fjavascript-wait%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You were on the right track with the setTimeout function:






      function hello() {
      alert("Hi!")
      setTimeout(function() {
      alert("Hi again!");
      }, 1000)

      }

      <button onclick="hello()">Say Hi!</button>








      share|improve this answer




























        2














        You were on the right track with the setTimeout function:






        function hello() {
        alert("Hi!")
        setTimeout(function() {
        alert("Hi again!");
        }, 1000)

        }

        <button onclick="hello()">Say Hi!</button>








        share|improve this answer


























          2












          2








          2







          You were on the right track with the setTimeout function:






          function hello() {
          alert("Hi!")
          setTimeout(function() {
          alert("Hi again!");
          }, 1000)

          }

          <button onclick="hello()">Say Hi!</button>








          share|improve this answer













          You were on the right track with the setTimeout function:






          function hello() {
          alert("Hi!")
          setTimeout(function() {
          alert("Hi again!");
          }, 1000)

          }

          <button onclick="hello()">Say Hi!</button>








          function hello() {
          alert("Hi!")
          setTimeout(function() {
          alert("Hi again!");
          }, 1000)

          }

          <button onclick="hello()">Say Hi!</button>





          function hello() {
          alert("Hi!")
          setTimeout(function() {
          alert("Hi again!");
          }, 1000)

          }

          <button onclick="hello()">Say Hi!</button>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 23:44









          MichaelvEMichaelvE

          1,3281311




          1,3281311

























              1














              An idiom that's becoming common as async/await is available in browsers it to make an async function and await a pause() function the returns a promise:






              let pause = (time) => new Promise(resolve => setTimeout(resolve, time))

              async function hello() {
              console.log("Hi!")
              await pause(2000)
              console.log("Hi again!");
              }

              <button onclick="hello()">Say Hi!</button>








              share|improve this answer




























                1














                An idiom that's becoming common as async/await is available in browsers it to make an async function and await a pause() function the returns a promise:






                let pause = (time) => new Promise(resolve => setTimeout(resolve, time))

                async function hello() {
                console.log("Hi!")
                await pause(2000)
                console.log("Hi again!");
                }

                <button onclick="hello()">Say Hi!</button>








                share|improve this answer


























                  1












                  1








                  1







                  An idiom that's becoming common as async/await is available in browsers it to make an async function and await a pause() function the returns a promise:






                  let pause = (time) => new Promise(resolve => setTimeout(resolve, time))

                  async function hello() {
                  console.log("Hi!")
                  await pause(2000)
                  console.log("Hi again!");
                  }

                  <button onclick="hello()">Say Hi!</button>








                  share|improve this answer













                  An idiom that's becoming common as async/await is available in browsers it to make an async function and await a pause() function the returns a promise:






                  let pause = (time) => new Promise(resolve => setTimeout(resolve, time))

                  async function hello() {
                  console.log("Hi!")
                  await pause(2000)
                  console.log("Hi again!");
                  }

                  <button onclick="hello()">Say Hi!</button>








                  let pause = (time) => new Promise(resolve => setTimeout(resolve, time))

                  async function hello() {
                  console.log("Hi!")
                  await pause(2000)
                  console.log("Hi again!");
                  }

                  <button onclick="hello()">Say Hi!</button>





                  let pause = (time) => new Promise(resolve => setTimeout(resolve, time))

                  async function hello() {
                  console.log("Hi!")
                  await pause(2000)
                  console.log("Hi again!");
                  }

                  <button onclick="hello()">Say Hi!</button>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 0:02









                  Mark MeyerMark Meyer

                  38.8k33159




                  38.8k33159






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403262%2fjavascript-wait%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents