Kar*_*ror 11 javascript counter countdown
在我的项目中,我有问题列表,因为每个问题都有三个选项答案.
看到问题后如果我想回答那个问题意味着点击"显示答案"按钮.当我点击按钮时,计数器会在显示一分钟错误后启动一分钟.
任何人都可以帮忙吗?
red*_*dow 19
你可以使用这样的东西:
function gameLost() {
alert("You lose!");
}
setTimeout(gameLost, 60000);
Run Code Online (Sandbox Code Playgroud)
UPDATE:传递函数引用setTimeout()而不是代码字符串(我真的这样写吗?O_o)
编辑
要显示计时器(改进版本,也要感谢davin):
<button onclick="onTimer()">Clickme</button>
<div id="mycounter"></div>
<script>
i = 60;
function onTimer() {
document.getElementById('mycounter').innerHTML = i;
i--;
if (i < 0) {
alert('You lose!');
}
else {
setTimeout(onTimer, 1000);
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
......
function timedOut() {
alert("Some error message");
}
// set a timer
setTimeout( timedOut , 60000 );
Run Code Online (Sandbox Code Playgroud)
这基本上设置了一个定时器,它将在60.000毫秒= 60秒= 1分钟后执行给定的功能
编辑:这是一个快速,不完美的小提琴,也显示倒计时http://jsfiddle.net/HRrYG
function countdown() {
var seconds = 60;
function tick() {
var counter = document.getElementById("counter");
seconds--;
counter.innerHTML = "0:" + (seconds < 10 ? "0" : "") + String(seconds);
if( seconds > 0 ) {
setTimeout(tick, 1000);
} else {
alert("Game over");
}
}
tick();
}
// start the countdown
countdown();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50209 次 |
| 最近记录: |