测验的javascript倒数计时器

时间:2016-07-15 09:19:52

标签: javascript timer countdown

我已经开发了一个测验。它可以使用javascript倒计时。倒计时工作正常,但也可以在提交表单时使用。脚本的使用只能在时间超过时才能工作。如何禁用脚本时我提交并让它只在你没时间的时候工作?代码如下:

<script type="text/javascript">
var count = 6;
var redirect = "http://www.apphp.com";

function countDown(){
var timer = document.getElementById("timer");
if(count > 0){
    count--;
    timer.innerHTML = "This page will redirect in "+count+" seconds.";
    setTimeout("countDown()", 1000);
}else{
    window.location.href = redirect;
}
}
</script>

Our webpage has beed moved. Please update your bookmarks for the new     site.
<br>

<span id="timer">
<script type="text/javascript">countDown();</script>
</span>

<a class="form_submit ss_btn" href="#" onclick="return SST.form_submit(widget_50467774);"> Submit </a> 

1 个答案:

答案 0 :(得分:0)

假设表单提交是ajax调用,您可以像这样清除超时:

var tId;
.
.
.
tId = setTimeout(countDown, 1000);
.
.
clearTimeout(tId);  // stop from running again

喜欢这里:

var count = 6;
var redirect = "http://www.apphp.com";
var tId;

function countDown() {
  var timer = document.getElementById("timer");
  if (count > 0) {
    count--;
    timer.innerHTML = "This page will redirect in " + count + " seconds.";
    tId = setTimeout(countDown, 1000);
  } else {
    window.location.href = redirect;
  }
}

由于您可能无权访问SST.form_submit,因此您可以添加此内联:

<a class="form_submit ss_btn" href="#"
onclick="clearTimeout(tId); return SST.form_submit(widget_50467774);"> Submit </a> 

或者如果提交中有阻止提交的测试(无法填写内容):

<a class="form_submit ss_btn" href="#"
onclick="var ok = SST.form_submit(widget_50467774); if (ok) clearTimeout(tId); return ok;"> Submit </a>