簡體   English   中英

如何通過jquery分別單獨管理多個setInterval函數

[英]How to manage many setInterval function by jquery each individually

嘿,我在類的所有div的每個函數中都有一個setInterval函數[參見下文],我的問題是對每個div的管理方式不同

請幫忙

var div_holder = $('div.products_each');
     div_holder.each(function(i){
var vari= setInterval(function() {
                  //do something here
          },1000/60)
});

我可以關閉這個

$(document).on("mouseenter", ".products_each_all",function(){
     $(this).children('div._title').css('margin-left',"0px");
        clearInterval(vari);
})

此Clear all setInterval調用[影響所有div操作]

我的問題是如何以不同方式管理每個類的setinterval

提前致謝

使用.data()分別存儲每個元素的間隔參考。

var div_holder = $('div.products_each');
div_holder.each(function (i) {
    var vari = setInterval(function () {
        //do something here
    }, 1000 / 60)
    $(this).data('vari', vari)
});

$(document).on("mouseenter", ".products_each_all", function () {
    $(this).children('div._title').css('margin-left', "0px");

    //each products_each element will have a data item called vari which holds the interval reference, you can use it to clear it later
    var div_holder = $('div.products_each');
    div_holder.each(function (i) {
        clearInterval($(this).data('vari'));
    });
})

您可以這樣實現:

$.each($(".products_each"), function (index, value) {
    var vari = setInterval(function() {
          // do what ever you want with value
          // it is your div : $(value).hide();
    }, 1000/60);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM