简体   繁体   English

按下“主页”按钮时,我试图清除倒数计时

[英]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.

相关问题 我试图在按下递增按钮时递增,但我得到 [object HTMLParagraphElement]1 - I am trying to increment when increment button is pressed but I am getting [object HTMLParagraphElement]1 我正在尝试制作一个清晰的按钮。 当用户单击清除按钮时,所有列表都应永久清除 - i am trying to make a clear button. when user click on the clear button then all list should be clear permanently 我正在尝试通过以下方式更改幻灯片的图片:按下按钮应将显示的图像更改为按下的按钮 - I am trying to get my slide show to change the picture by: pressing a button should change the displayed image to the button pressed 按下特定按钮时如何清除setInterval? - How can I clear setInterval when a specific button is pressed? 当按下按钮时,我无法在angular js中生成警报 - I am not able to generate alert in angular js when a button is pressed 在Django中按下清除按钮时清除输入文件 - clear input filed when clear button is pressed in Django 当您使用Jquery按下按钮时,我试图使页面滚动速度变慢吗? - I am trying to make my page scroll slower when you press a button using Jquery? 我试图在单击按钮时停止两次显示同一对象的功能 - I am trying to stop my function from displaying the same object twice when clicking a button 在Phonegap / iOS中按下主页按钮时访问localStorage - Accessing localStorage when home button is pressed in Phonegap/iOS 为什么在尝试对列表进行排序时单击按钮时list.js不能正常工作 - Why isn't list.js working when I click the button when I am trying to sort my list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM