I'd like my JavaScript to, at the end of the function I have created, wait seven seconds, and then refresh my page. If it is important, I have the vital parts of my JavaScript and HTML below...
Javascript:
var textfill = function () {
var node = document.createElement("P");
var x = document.getElementById('entertext').value;
var textnode = document.createTextNode("The search results for: '" + x + "' will show up here");
node.appendChild(textnode);
document.getElementById("123").appendChild(node);
}
HTML:
<input type="text" id="entertext">
<input type="button" onclick="textfill()" value="Search">
<p id="123">
</p>
I'd like my JavaScript to, at the end of the function I have created, wait seven seconds, and then refresh my page. If it is important, I have the vital parts of my JavaScript and HTML below...
Javascript:
var textfill = function () {
var node = document.createElement("P");
var x = document.getElementById('entertext').value;
var textnode = document.createTextNode("The search results for: '" + x + "' will show up here");
node.appendChild(textnode);
document.getElementById("123").appendChild(node);
}
HTML:
<input type="text" id="entertext">
<input type="button" onclick="textfill()" value="Search">
<p id="123">
</p>
Share
Improve this question
edited Sep 24, 2015 at 3:14
AHolasek
asked Sep 24, 2015 at 3:13
AHolasekAHolasek
2113 silver badges11 bronze badges
5
- Yes, but I'm not sure how to make that start once the function is finished, and how to have it trigger a refresh. – AHolasek Commented Sep 24, 2015 at 3:15
- Add it at the end of the function – Tushar Commented Sep 24, 2015 at 3:16
- Well, yeah, but to have it trigger a refresh? – AHolasek Commented Sep 24, 2015 at 3:16
- Have a look at this stackoverflow./questions/7854820/… – Pallavi Commented Sep 24, 2015 at 3:16
- What you really want to do is to submit the search form, not just refresh the page. – xdazz Commented Sep 24, 2015 at 3:19
2 Answers
Reset to default 5function refreshPage() {
//ensure reloading from server instead of cache
location.reload(true);
}
function delayRefreshPage(mileSeconds) {
window.setTimeout(refreshPage, mileSeconds);
}
var textfill = function () {
var node = document.createElement("P");
var x = document.getElementById('entertext').value;
var textnode = document.createTextNode("The search results for: '" + x + "' will show up here");
node.appendChild(textnode);
document.getElementById("123").appendChild(node);
delayRefreshPage(2000);
}
Summarizing @ioseph and my personal experience.
To do something after a certain amount of time use setTimeout - https://developer.mozilla/en-US/docs/Web/API/WindowTimers/setTimeout
And to refresh the page, call
window.location.reload
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742311738a4420028.html
评论列表(0条)