简体   繁体   English

JavaScript倒数表单提交不起作用

[英]Javascript countdown form submit not working

So I was trying to write some javascript to countdown and then submit the form when the countdown is finished. 所以我试图写一些JavaScript到倒数,然后在倒数完成时提交表单。 This is the JS I found online and tried to implement: 这是我在网上找到并尝试实现的JS:

 var seconds = 300;

 function secondPassed() {
   var minutes = Math.round((seconds - 30) / 60),
     remainingSeconds = seconds % 60;

   if (remainingSeconds < 10) {
     remainingSeconds = "0" + remainingSeconds;
   }

   document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
   if (seconds == 0) {
     clearInterval(countdownTimer);
     document.qForm.submit();
   } else {
     seconds--;
   }
 }
 var countdownTimer = setInterval('secondPassed()', 1000);

Here it is implemented: https://jsfiddle.net/spadez/9p9o4k6s/1/ 在这里实现: https : //jsfiddle.net/spadez/9p9o4k6s/1/

The error says: secondPassed is not defined but I'm not sure why that is. 错误消息: 未定义secondPassed,但我不确定为什么这样做。 Could anyone please explain where I have gone wrong? 谁能解释我哪里出错了?

There is an error in last line of your code. 代码的最后一行有错误。 Change that line to this' 将该行更改为此'

var countdownTimer = setInterval(secondPassed, 1000);

Here is the updated fiddle. 这是更新的小提琴。 https://jsfiddle.net/9p9o4k6s/3/ https://jsfiddle.net/9p9o4k6s/3/

When you pass a function call as a string to setInterval like that, the function you're calling must be in the global scope. 当像这样将函数调用作为字符串传递给setInterval ,要调用的函数必须在全局范围内。 Your function secondPassed is wrapped by the jsFiddle onload wrapper and so it is not in the global scope. 您的函数secondPassed由jsFiddle onload包装器包装,因此不在全局范围内。

To demonstrate this, if you did window.secondPassed = secondPassed; 为了证明这一点,如果您做了window.secondPassed = secondPassed; to put it in the global scope, your existing code would work. 要将其放在全局范围内,您现有的代码将可以正常工作。

The proper solution is to pass the function object as the argument instead of a string: 正确的解决方案是将函数对象作为参数而不是字符串:

var countdownTimer = setInterval(secondPassed, 1000);

Note there are no parenthesis () when used in this way. 请注意,以这种方式使用时,没有括号()

For me both ways of calling setInterval work well with SO snippet. 对我来说,调用setInterval两种方法都可以与SO代码段一起很好地工作。 I think bug can be jsfiddle specific as it seems it accepts only the first form of setInterval , which takes function and interval, not code string to execute and delay ( https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval ). 我认为Bug可能是jsfiddle特有的,因为它似乎只接受setInterval的第一种形式,该形式需要函数和间隔,而不是要执行和延迟的代码字符串( https://developer.mozilla.org/en-US/docs/Web / API / WindowOrWorkerGlobalScope / setInterval )。 If you want to keep using fiddle you will have to change last line to: 如果要继续使用小提琴,则必须将最后一行更改为:

var countdownTimer = setInterval(secondPassed, 1000);

  var seconds = 300; function secondPassed() { var minutes = Math.round((seconds - 30) / 60), remainingSeconds = seconds % 60; if (remainingSeconds < 10) { remainingSeconds = "0" + remainingSeconds; } document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds; if (seconds == 0) { clearInterval(countdownTimer); document.qForm.submit(); } else { seconds--; } } var countdownTimer = setInterval('secondPassed()', 1000); 
 body { background-color: #eee; } canvas {} time {color: red; float: right;} .active {color: green; font-size: 12px; font-weight: bold;} 
  <h3>Question 1 of 20<time id="countdown">5:00</time></h3> <form name="qForm"> <input type="radio"> 219 <input type="submit" value="Submit"> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM