admin管理员组文章数量:1403371
I have a small C# code behind to refresh a webform page (form.aspx) after 15 seconds as below:
lblMessage.Text = "<b><h2>Thank you!</h2></b></br>We will be right with you.<br><br><br>(This page will be refreshed automatically after 15 seconds)";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success!", "setInterval(function(){location.href='/form.aspx';},15000);", true);
Right now the page will be refreshed after 15 seconds. How do I also make the timer count down every second? i.e., 15 => 14 => 13 => ... 1 then refresh so it will be better for users, they will know what is going on with the page...
I have a small C# code behind to refresh a webform page (form.aspx) after 15 seconds as below:
lblMessage.Text = "<b><h2>Thank you!</h2></b></br>We will be right with you.<br><br><br>(This page will be refreshed automatically after 15 seconds)";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success!", "setInterval(function(){location.href='/form.aspx';},15000);", true);
Right now the page will be refreshed after 15 seconds. How do I also make the timer count down every second? i.e., 15 => 14 => 13 => ... 1 then refresh so it will be better for users, they will know what is going on with the page...
Share Improve this question edited Jan 7, 2015 at 22:01 Ronaldinho Learn Coding asked Jan 7, 2015 at 20:34 Ronaldinho Learn CodingRonaldinho Learn Coding 13.8k25 gold badges86 silver badges111 bronze badges 2- Create an input or element on the page, that shows the timer counting down on each interval. – Mouser Commented Jan 7, 2015 at 20:42
- 1 I think this would be better suited and easier to understand putting it in your aspx rather than code behind – Kritner Commented Jan 7, 2015 at 20:47
2 Answers
Reset to default 6In javascript I would go with something like this.
<div id='countdown'></div.
var countDown = 15;
function countdown() {
setInterval(function () {
if (countDown == 0) {
return;
}
countDown--;
document.getElementById('countdown').innerHTML = countDown;
return countDown;
}, 1000);
}
countdown();
Fiddle: http://jsfiddle/chzzcosy/
"<b><h2>Thank you!</h2></b></br>We will be right with you.<br><br><br>(This page will be refreshed automatically after <span id='counter'>15</span> seconds)"
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success!", "setTimeout(function(){location.href='/form.aspx';},15000); counter = 15; setInterval(function(){counter--; document.getElementById('counter').innerHTML = counter;},1000);", true);
Should do it.
If added a span
with id
counter
around the refresh message number.
Then I added counter = 15;
to initialize a default value of 15. And another setInterval
to the script block firing every second. With each pass it subtracts one from counter
and updates the span
with the new counter value. Users should now see the page counting down. I also changed the first setInterval
to setTimout
, since it's technically a timeout and not an interval that should occur every 15 seconds.
本文标签: javascriptAuto refresh a page with count down timeStack Overflow
版权声明:本文标题:javascript - Auto refresh a page with count down time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744412982a2605051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论