Javascript Wait
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>
javascript html wait
add a comment |
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>
javascript html wait
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 aftermyFunction
. 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
add a comment |
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>
javascript html wait
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
javascript html wait
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 aftermyFunction
. 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
add a comment |
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 aftermyFunction
. 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
add a comment |
2 Answers
2
active
oldest
votes
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>
add a comment |
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>
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
add a comment |
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>
add a comment |
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>
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>
answered Nov 20 '18 at 23:44
MichaelvEMichaelvE
1,3281311
1,3281311
add a comment |
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
answered Nov 21 '18 at 0:02
Mark MeyerMark Meyer
38.8k33159
38.8k33159
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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