[英]I am trying to clear my countdown when the 'home' button is pressed
so, my issue is that I am trying to get my countdown to clear when i hit the 'back' icon. 因此,我的问题是,当我点击“后退”图标时,我试图清除倒数。 This is so that if the user clicks back to the timer page it will be reset, not still continuing from when they hit back.
这样一来,如果用户单击返回到计时器页面,它将被重置,而不是从他们返回时继续。
This is the line of code that i think should be doing the trick: 这是我认为应该达到目的的代码行:
$('.ui-icon-back').click (clearInterval(countdown));
here is my HTML: 这是我的HTML:
<!-- /////////////////
Green Tea
////////////////////// -->
<!--Create section tag-->
<section data-role="page" id="green">
<!--Create header tag-->
<header data-role="header">
<!--Create h1 tag-->
<h1> TEA TIME </h1>
<!--Create a icon that will link back to the home page-->
<a href="#home" class="ui-btn ui-btn-icon-top ui-icon-back ui-btn-icon-notext">back</a>
<!--End header-->
</header>
<!--Create h1 tag that states full steep time-->
<h1>Green Tea Takes 2 Minutes</h1>
<!--Show timer duration before timer start-->
<p id="timedisp">120 sec</p>
<!--Call the countdown-->
<div class="clock">
</div>
<!-- Button to trigger the start timer js-->
<a href="#" id="start">Start</a>
<!-- Button to trigger the timer restart-->
<a href="#" id="reset">Reset</a>
<!-- End section tag-->
</section>
Here is my Javascript: 这是我的Javascript:
// JavaScript Document
//Green Tea Timer
function greenTea(){
// Set The Duration
var duration = 120;
// Insert the duration into the div with a class of clock
$(".clock").html(duration + " sec");
// Create a countdown interval
var countdown = setInterval(function (greenTea) {
// subtract one from duration and test to see
// if duration is still above zero
if (--duration) {
// Update the clocks's message
$(".clock").html(duration + " sec");
// Otherwise
} else {
// Clear the countdown interval
clearInterval(countdown);
// set a completed message
$(".clock").html("End Your Steep");
}
// Run interval every 1000ms
}, 1000);
};
$("a#start").click(greenTea);
$('#start').click(function(greenTea){
$('#timedisp').hide();
});
$('#reset').click(function(greenTea) {
location.reload();
});
$('.ui-icon-back').click (clearInterval(countdown));
When you do: $(..).click(clearInterval(..));
当您这样做时:
$(..).click(clearInterval(..));
JavaScript is immediately executing the clearInterval method. JavaScript立即执行clearInterval方法。 You need to do:
您需要做:
$(..).click(function(){
clearInterval(..);
});
After that, you need to put that code inside greenTea function for the clearInterval to be able to access the countdown
variable. 之后,您需要将该代码放入greenTea函数中,以使clearInterval能够访问
countdown
变量。 That or declare the variable countdown
outside greanTea. 或在greanTea之外声明变量
countdown
。 I recommend to do the first one because the second one will pollute the global scope. 我建议执行第一个操作,因为第二个操作会污染全局范围。
The countdown
variable is declared inside the greenTea
function. countdown
变量在greenTea
函数中声明。 You have to declare it outside the function for it to be accessible from the click handler. 您必须在函数外部声明它,才能从单击处理程序访问它。
You'll also have to put your jQuery functions inside the jQuery ready function: 您还必须将jQuery函数放入jQuery ready函数中:
var countdown;
function greenTea(){
var duration = 120;
$(".clock").html(duration + " sec");
countdown = setInterval(function (greenTea) {
if (--duration) {
$(".clock").html(duration + " sec");
} else {
clearInterval(countdown);
$(".clock").html("End Your Steep");
}
}, 1000);
};
$(function(){
$("a#start").click(greenTea);
$('#start').click(function(greenTea){
$('#timedisp').hide();
});
$('#reset').click(function(greenTea) {
location.reload();
});
$('.ui-icon-back').click (function(){
clearInterval(countdown));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.