简体   繁体   中英

How to manage many setInterval function by jquery each individually

hey i have a setInterval function [see below] inside a an each function for all divs with class, my problem is to manage each divs differently

please help

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

and i could close this by

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

this Clear all setInterval call [effects all div action]

My question is how to manage each classes setinterval differently

thanks in advance

use .data() to store the interval reference of each element individually.

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'));
    });
})

you can achieve it like this:

$.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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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