简体   繁体   English

如何通过jquery分别单独管理多个setInterval函数

[英]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 嘿,我在类的所有div的每个函数中都有一个setInterval函数[参见下文],我的问题是对每个div的管理方式不同

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] 此Clear all setInterval调用[影响所有div操作]

My question is how to manage each classes setinterval differently 我的问题是如何以不同方式管理每个类的setinterval

thanks in advance 提前致谢

use .data() to store the interval reference of each element individually. 使用.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'));
    });
})

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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