0

使用最新代码更新:

这是最新的代码,仍然有 2 个按钮和 2 个功能。每个按钮都应该运行相关函数,并通过 setInterval 将代码设置为每分钟后运行相同的按钮。出于某种原因,第一个 setInterval 似乎总是保持设置,即使在单击应该更改它的第二个按钮之后也是如此。不知道我做错了什么:

$(document).ready(function() {
    var myTimer = null;

    get_popular();

    clearInterval(myTimer);
    myTimer = null;
    myTimer = setInterval(function() {$('a.btnPopular').click();}, 60*1000);

    $('a.btnPopular').click( function(event) {
        event.preventDefault(); 
        get_popular( $(this) );

        clearInterval(myTimer);
        myTimer = null;
        myTimer = setInterval(function() {$('a.btnPopular').click();}, 60*1000);
    });

    function get_popular( that ) {
        $.ajax({
            url: 'get_popular.aspx?rand=' + Math.random(),
            type: 'GET',
            error: function(xhr, status, error) {
                // error message here
            },
            success: function(results) { 
                if ( typeof that != "undefined" ) {
                    that.closest('.outer_div').find('.inner_div').empty().append( results );
                } else {
                    $('.inner_div.new').empty().append( results ).removeClass('new');
                }
            }
        });
    }

    $('a.btnLatest').click( function(event) {
        event.preventDefault(); 
        get_latest( $(this) );

        clearInterval(myTimer);
        myTimer = null;
        myTimer = setInterval(function() {$('a.btnLatest').click();}, 60*1000);
    });

    function get_latest( that ) {
        $.ajax({
            url: 'get_latest.aspx?rand=' + Math.random(),
            type: 'GET',
            error: function(xhr, status, error) {
                // error message here
            },
            success: function(results) { 
                if ( typeof that != "undefined" ) {
                    that.closest('.outer_div').find('.inner_div').empty().append( results );
                } else {
                    $('.inner_div.new').empty().append( results ).removeClass('new');
                }
            }
        });
    }
});
4

2 回答 2

1

尝试:

setInterval(function() {$('a.btnPopular').click();}, 60*1000);

编辑:你也不能get_popular();在没有参数的情况下调用;)

编辑2:这应该可以解决您的问题:

var myTimer = null;

$('a.btnPopular').click(function(event) {
    event.preventDefault(); 
    get_popular($(this));
    clearTimeout(myTimer);
    myTimer = setTimeout(function(){$('a.btnPopular').click();}, 60*1000);
});

$('a.btnLatest').click(function(event) {
    event.preventDefault(); 
    get_latest($(this));
    clearTimeout(myTimer);
    myTimer = setTimeout(function(){$('a.btnLatest').click();}, 60*1000);
});
于 2012-09-27T11:17:06.723 回答
0
setInterval(function() {$('a.btnPopular').trigger('click');}, 60*1000);
// Ad do this
$(elem).click(function(){}) => $(elem).on('click', function(){});
于 2012-09-27T11:20:13.270 回答