简体   繁体   English

尝试使用jQuery当用户到达页面底部时停止计时器

[英]trying to make a timer stop when the user reaches the bottom of the page using jQuery

I have a timer that starts counting up when the page is loaded. 我有一个计时器,在页面加载时开始计时。 I want it to stop when the user scrolls to the bottom of the page. 我希望它在用户滚动到页面底部时停止。 Here is the jQuery I've written: 这是我编写的jQuery:

function timerTick(time, stop)
{
    if(stop == false)
    {
        setInterval(function () 
        {
            time += 1;
            var displayTime = time/10;
            if(displayTime % 1 != 0)
            {
                $('.time').text(displayTime.toString());
            }
            else
            {
                $('.time').text(displayTime.toString() + ".0");
            } 

        }, 100);
    }
    else //behavior is the same if i remove the else block
    {
        return;
    }
}

$(document).ready(function () {
    var time = 0;
    var stop = false;

    timerTick(time, stop);

    //check if we're at the bottom
    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() == $(document).height()) {
            stop = true;
        }
    });


});

The timer counts up perfectly, the problem is I can't get it to stop. 计时器的计数非常完美,问题是我无法停止计时。 If I replace the stop = true; 如果我替换stop = true; line with alert('abc'); 符合alert('abc'); , the alert shows up when the user reaches the bottom. ,当用户到达底部时,警报就会显示出来。 So all of the pieces are working, just for some reason setting stop to true doesn't stop the timerTick function from going into the if(stop == false) block. 因此所有部分都在工作,只是出于某种原因将stop设置为true不会阻止timerTick函数进入if(stop == false)块。

Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?


Edit: I made a jsFiddle . 编辑:我做了一个jsFiddle

You have to clear interval as soos as user reach the end of page. 当用户到达页面末尾时,您必须清除间隔。 Otherwise it will continue executing. 否则它将继续执行。

Try: 尝试:

var intervalId;

    function timerTick(time, stop)
    {
        if(stop == false)
        {
            intervalId=setInterval(function () //Set the interval in a var
            {
                time += 1;
                var displayTime = time/10;
                if(displayTime % 1 != 0)
                {
                    $('.time').text(displayTime.toString());
                }
                else
                {
                    $('.time').text(displayTime.toString() + ".0");
                } 

            }, 100);
        }
        else //behavior is the same if i remove the else block
        {

            return;
        }
    }

    $(document).ready(function () {
        var time = 0;
        var stop = false;

        timerTick(time, stop);

        //check if we're at the bottom
        $(window).scroll(function() {
            if($(window).scrollTop() + $(window).height() == $(document).height()) {
                stop = true;
                clearInterval(intervalId);//HERE clear the interval
            }
        });


    });

DEMO DEMO

to determine bottom of the page you could try this 确定页面底部,您可以尝试此操作

if(window.innerHeight + document.body.scrollTop >= document.body.offsetHeight) {
    stop = true;
}

Update: 更新:
you need to make your variable stop global, declare it outside of documnet.ready function. 您需要使变量全局stop ,并在documnet.ready函数外部声明它。

setInterval函数返回数字,您以后可以将其传递给clearInterval以停止计时器。

Declare 宣布

var Timeout=0;

check 校验

 if(stop == false) 

inside the setInterval function 在setInterval函数中

like 喜欢

  Timeout=setInterval(function () 
    {

       if(stop == false) 
            {
        time += 1;
        var displayTime = time/10;
        if(displayTime % 1 != 0)
        {
            $('.time').text(displayTime.toString());
        }
        else
        {
            $('.time').text(displayTime.toString() + ".0");
        } 
             }
       else

             {
            clearInterval(Timeout);
             }

    }, 100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM