如何用按钮激活我的倒数计时器?
how can activate my countdown timer with button?
我在 javascript.Its 中编写了一个倒数计时器功能,但它会在我启动时启动 page.I 希望它应该在单击按钮时启动。
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
// timer = 0;
time.style.display = "none";
time2.style.display2 = "none";
redxsms.style.display = "none";
onaysms.style.display = "none";
tekraruyarionay.style.display = "block";
tekraruyarired.style.display = "block";
// time.style.display="block";
setTimeout(function () { location.reload() }, 5000);
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 60 * 3,
display = $('#time');
display2 = $('#time2');
startTimer(fiveMinutes, display);
startTimer(fiveMinutes, display2);
});
这是倒计时功能。
<button class="ui toggle button" onclick=" hideshow(document.getElementById('onaysms')); show(this);" id="onaytoggle">
这是按钮,当我点击它时它应该被激活。
<span id="time"></span>
它在页面启动时启动 如何在单击按钮时启动?
像这样在 "onclick" 事件中插入您的计时器调用,当您单击按钮时它会被触发。
如果您的页面上有多个按钮,我建议您通过在 html 中添加以下内容来给一个 ID:id='timerToggle'
然后替换 $("button.toggle")
$("#timerToggle")
下面的代码
jQuery(function ($) {
$("button.toggle").click(function(){
var fiveMinutes = 60 * 3,
display = $('#time');
display2 = $('#time2');
startTimer(fiveMinutes, display);
startTimer(fiveMinutes, display2);
});
});
我在 javascript.Its 中编写了一个倒数计时器功能,但它会在我启动时启动 page.I 希望它应该在单击按钮时启动。
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
// timer = 0;
time.style.display = "none";
time2.style.display2 = "none";
redxsms.style.display = "none";
onaysms.style.display = "none";
tekraruyarionay.style.display = "block";
tekraruyarired.style.display = "block";
// time.style.display="block";
setTimeout(function () { location.reload() }, 5000);
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 60 * 3,
display = $('#time');
display2 = $('#time2');
startTimer(fiveMinutes, display);
startTimer(fiveMinutes, display2);
});
这是倒计时功能。
<button class="ui toggle button" onclick=" hideshow(document.getElementById('onaysms')); show(this);" id="onaytoggle">
这是按钮,当我点击它时它应该被激活。
<span id="time"></span>
它在页面启动时启动 如何在单击按钮时启动?
像这样在 "onclick" 事件中插入您的计时器调用,当您单击按钮时它会被触发。
如果您的页面上有多个按钮,我建议您通过在 html 中添加以下内容来给一个 ID:id='timerToggle'
然后替换 $("button.toggle")
$("#timerToggle")
jQuery(function ($) {
$("button.toggle").click(function(){
var fiveMinutes = 60 * 3,
display = $('#time');
display2 = $('#time2');
startTimer(fiveMinutes, display);
startTimer(fiveMinutes, display2);
});
});