简体   繁体   English

超时后的jQuery火灾事件

[英]jquery fire event after timeout

I've implemented auto page scrolling using the jQuery plugin datatables . 我已经使用jQuery插件实现自动页面滚动的数据表

I'm attempting to add two buttons, one that stops the auto scrolling and one that starts it again. 我试图添加两个按钮,一个按钮停止自动滚动,另一个按钮再次启动。 I've gotten this working, but I want it to begin scrolling again if the page has been stopped for a period of time. 我已经开始工作了,但是如果页面已停止一段时间,我希望它再次开始滚动。 Here is what I have tried: 这是我尝试过的:

var refreshRate = 5;
var totalPages = 0;
var currentPage = 0;
var upTime = 0;

$('document').ready(function(){
var interval = window.setInterval('$("#start").click()',refreshRate);

    $("#start").click(function(){
        function start(){
            clearInterval(timeout);
            $("#stop").show();
            $("#start").hide();
            currentPage = table.api().page.info().page + 1;
            totalPages = table.api().page.info().pages;
            table.api().ajax.reload(function(json){

                if (currentPage != totalPages){
                    table.api().page('next').draw(false);
                }
                else{
                    table.api().page('first').draw(false);
                }
            });

            upTime = upTime + (refreshRate / 1000);
            clearInterval(interval);
            interval = setInterval('$("#start").click()', refreshRate);
        }
        start();
    });

    $("#stop").click(function(){
        $("#stop").hide();
        $("#start").show();
        timeout();

        clearInterval(interval);

    });

//call start on doc ready to auto start scrolling
    start();

    function timeout(){
        var timeout = window.setInterval(function(){


            start();
        }, 10000);
    }
});

As you can see I am attempting to call the start() function embedded within the button click after a 10 second timeout. 如您所见,我试图在10秒超时后调用按钮click内嵌的start()函数。 I am also getting a TypeError when I call start() at DOM ready to begin page scrolling. 当我在DOM处调用start()准备开始页面滚动时,我也遇到TypeError。

Uncaught TypeError: object is not a function

But besides the error it scrolls as it should. 但是,除了错误以外,它还会滚动。 Any assistance? 有什么帮助吗?

You are having issues with function scope. 您在功能范围方面遇到问题。 Your code that called start towards the bottom did not have start in its scope since you defined it inside the click handler. 您的代码调用start向底部没有start在其范围内,因为你里面定义它click处理程序。 Also, timeout was outside of the scope too, which would cause the interval to go on forever: 另外, timeout也超出了范围,这将导致间隔永远持续下去:

$('document').ready(function(){
    var timeout,
        interval = window.setInterval('$("#start").click()',refreshRate);
    function start(){
            clearInterval(timeout);
            $("#stop").show();
            $("#start").hide();
            currentPage = table.api().page.info().page + 1;
            totalPages = table.api().page.info().pages;
            table.api().ajax.reload(function(json){

                if (currentPage != totalPages){
                    table.api().page('next').draw(false);
                }
                else{
                    table.api().page('first').draw(false);
                }
            });

            upTime = upTime + (refreshRate / 1000);
            clearInterval(interval);
            interval = setInterval('$("#start").click()', refreshRate);
    }

    function timeout(){
        timeout = setInterval(function(){
            start();
        }, 10000);
    }

    $("#start").click(function(){            
        start();
    });

    $("#stop").click(function(){
        $("#stop").hide();
        $("#start").show();
        timeout();    
        clearInterval(interval);

    });

    //call start on doc ready to auto start scrolling
    start();    
});

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

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