在点击按钮时无法停止此倒计时,从我读过def wrapper_get_terminal_size():
"""
Replace the original function termui.get_terminal_size (click lib) by a new one
that uses a fallback if ValueError exception has been raised
"""
from click import termui, formatting
old_get_term_size = termui.get_terminal_size
def _wrapped_get_terminal_size():
try:
return old_get_term_size()
except ValueError:
import os
sz = os.get_terminal_size()
return sz.columns, sz.lines
termui.get_terminal_size = _wrapped_get_terminal_size
formatting.get_terminal_size = _wrapped_get_terminal_size
似乎是最好的路线。我希望它显示一旦点击停止的数字,所以我可以稍后将它放在SQL表中。这意味着消极,一旦它,我有一个skull and crossbones picture 应该每隔一秒弹出,重叠计时器。
为什么按钮点击时倒数停止?
固定
clearTimeout()
var seconds = 10;
function stopTimer() {
clearTimeout(countdownTimer)
}
function floor(x) {
return x | 0;
}
function pad(n) {
if (n < 0) {
n = -n;
}
if (n < 10) {
return '0' + n.toString();
}
return n.toString();
}
function secondPassed() {
var minutes = pad(floor(seconds/60));
if (seconds < 0) {
minutes = '-' + minutes;
}
var remainingSeconds = pad(seconds % 60);
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
if (seconds > 0) {
seconds--;
if (seconds > 8) {document.body.style.backgroundColor = "green"; }
else if (seconds == 5) {document.body.style.backgroundColor = "yellow";}
else if (minutes == 0 & seconds == 0) {document.body.style.backgroundColor = "red";}
}
else{
seconds--;
if (seconds == 0 || seconds == 2 || seconds == 4 || seconds == 6 || seconds == 8 ) {document.getElementById("Image1").style.visibility= "visible";}
else {document.getElementById("Image1").style.visibility= "hidden";}
}
}
#bigfuckingspoon
{
position:absolute;
width:600px;
height:500px;
z-index:0;
}
#littlespoon
{
z-index:1;
}
答案 0 :(得分:0)
stop函数中无法访问countdownTimer var。删除按钮onclick方法中的var
关键字。
<button onclick="countdownTimer = setInterval('secondPassed()', 1000)">Start</button>
答案 1 :(得分:0)
<button onclick="var countdownTimer = setInterval('secondPassed()', 1000)">Start</button>
使用变量为当前函数的var
范围。在onclick
中使用时,我猜它只在那里可用。移除var
以使其成为全局,然后clearTimeout(countdownTimer)
可以看到countdownTimer
变量