html - How do I get JavaScript to delay, and then refresh the page - Stack Overflow

I'd like my JavaScript to, at the end of the function I have created, wait seven seconds, and then

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
Add a ment  | 

2 Answers 2

Reset to default 5
function 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信