Javascript autoclick loop not reset when clicked again

I did a little script for Tampermonkey to help me browse tumblr notes without a flick of a click or look at the “user liked” spam.

$('.more_notes_link').click(function() {
    var LoopCount = 10;
    var i = 0;
    clearInterval(loadNotesInterval);
    var loadNotesInterval = setInterval(function() {
        if ($('.more_notes_link').length ) {
            $('.more_notes_link').click();
            i++;
            console.log(i);
            if (i >= LoopCount) {
                clearInterval(loadNotesInterval);
            }
        }
        else
        {
            clearInterval(loadNotesInterval);
        }
    }, 1000)
});

GM_addStyle ( ".notes .note.without_commentary{display:none} .notes .note.reply{display:inline}" );

For some reason, however, it just works fine when it starts up once, writing down numbers from 1 to 10, and then stopping ... And then it doesn't work anymore. There is no console output, no cycles, and it only clicks once.

I tried and refused it several times, so I thought that finally I would just ask if anyone could fix my problem. I can’t figure it out myself.

EDIT: console.log , , script . - , .more_notes_link , , .

+4
3

!

$('body').on('click', '.more_notes_link',function() {
    if (i == 0) {
        clearInterval(loadNotesInterval);
        var loadNotesInterval = setInterval(function() {
            if ($('.more_notes_link').length) {
                i++;
                $('.more_notes_link').click();
                if (i >= LoopCount) {
                    i = 0;
                    clearInterval(loadNotesInterval);
                }
            }
            else
            {
                i = 0;
                clearInterval(loadNotesInterval);
            }
        }, 1000)
    }
});

-, , , .

, . , , .

, - , .

- , !

+1

loadNotesInterval - , .

, var, , .

(function () {
    var loadNotesInterval;
    $('.more_notes_link').click(function() {
        var LoopCount = 10;
        var i = 0;
        clearInterval(loadNotesInterval);
        loadNotesInterval = setInterval(function() {
            if ($('.more_notes_link').length ) {
                $('.more_notes_link').click();
                i++;
                console.log(i);
                if (i >= LoopCount) {
                    clearInterval(loadNotesInterval);
                }
            }
            else
            {
                clearInterval(loadNotesInterval);
            }
        }, 1000)
    });    
}());
+1

, , , , .

, on click. .

$('document').on('click', '.more_notes_link', function() {
  // your code
}
+1

Source: https://habr.com/ru/post/1626272/


All Articles