Привязать setInterval () к элементу jQuery

Если вы указываете сценарий для создания контейнера со статическим именем - например, в вашем случае - он не будет воссоздан, так как модуль AzureRM обнаруживает, что указанная группа контейнеров уже существует. Попробуйте добавить «Remove-AzureRmContainerGroup ...» в одну строку над «New-AzureRmContainerGroup ...»

0
задан Swen 19 January 2019 в 14:23
поделиться

2 ответа

Рассмотрим объявление глобальной переменной.

// Start notification countdown
$.countDownNotification = function(notification) {
    // IMPORTANT: Store the setInterval in a element-specific variable?
    timer = setInterval( function() {
        // Counts down from 10 and stores new value in data-attribute
        notification.attr('data-timer', notification.attr('data-timer') - 1);
    }, 1000);

    // Remove notification when timer is on 0
    if ( newRemaining == 0 ) {
        notification.remove();
    }
}

// `false` means no timer has been set
var timer = false;

// Pause on hover
$('.notification').on('mouseenter', function(e) {
    // IMPORTANT: Clear the elemnt-specific interval
    clearInterval( timer );
});

// Resume when hover ends
$('.notification').on('mouseleave', function(e) {
    var notification = $(this)

    $.countDownNotification(notification);
});

Другой способ не устанавливать глобальный объект - это вернуть setInterval() на .countDownNotification.

// Start notification countdown
$.countDownNotification = function(notification) {
    // IMPORTANT: Store the setInterval in a element-specific variable?
    var id = setInterval( function() {
        // Counts down from 10 and stores new value in data-attribute
        notification.attr('data-timer', notification.attr('data-timer') - 1);
    }, 1000);

    // Remove notification when timer is on 0
    if ( newRemaining == 0 ) {
        notification.remove();
    }

    return id;

}

( function() {

    // `false` means no timer has been set
    var timer = false;

    // Pause on hover
    $('.notification').on('mouseenter', function(e) {
        // IMPORTANT: Clear the elemnt-specific interval
        clearInterval( timer );
    });

    // Resume when hover ends
    $('.notification').on('mouseleave', function(e) {
        var notification = $(this)

        timer = $.countDownNotification(notification);
    });

})();
0
ответ дан mrReiha 19 January 2019 в 14:23
поделиться

Вы можете сохранить интервал в уведомлении через .data().

notification.data('int', setInterval(...

Затем, в обратных вызовах событий вы можете ссылаться на интервал через

$(this).data('int')

Также, примечание + 1 - 1 не делает ничего значимого.

0
ответ дан Utkanos 19 January 2019 в 14:23
поделиться
Другие вопросы по тегам:

Похожие вопросы: