gpt4 book ai didi

javascript - 动态设置超时

转载 作者:行者123 更新时间:2023-12-03 06:04:01 25 4
gpt4 key购买 nike

如何清理超时的几个hover元素?

我的代码如下:

$(document).ready(function(){
$(".imagenesC").hover(imagenesCEntrada,imagenesCSalida);
});

var timeouts = {};
function imagenesCEntrada(){
var id=$(this).attr("id");
clearTimeout(timeouts['cuadricula'][id]);
$("#"+id).stop(true).animate({"opacity":"0"},1000);
}

function imagenesCSalida(){
var id=$(this).attr("id");
timeouts['cuadricula'][id] = setTimeout(function(){
$("#"+id).stop(true).animate({"opacity":"1"},1000);
}, 100);
}

一句问候

最佳答案

似乎您有多个具有相同类的元素,您可以使用它们的索引来创建动态计时器数组。

参见工作片段。

var timer = [];

$('.label').mouseenter(function(){
clearTimeout(timer[$(this).index()]);
console.log("Mouse enter");
$('#' + this.id + ' div').slideDown('slow');
});
$('.label').mouseleave(function(){
var temp = $('#' + this.id + ' div');
timer[$(this).index()] = setTimeout(function() {
console.log("Mouse leave");
temp.stop().slideUp('slow');
}, 2000);
});


$(document).ready(function(){
$(".imagenesC").hover(imagenesCEntrada,imagenesCSalida);
});

var timeouts = [];
function imagenesCEntrada(){
var id=$(this).attr("id");
clearTimeout(timeouts[$(this).index()]);
console.log("clear");
$("#"+id).stop(true).animate({"opacity":"0"},1000);
}

function imagenesCSalida(){
var id=$(this).attr("id");
console.log("enter");
timeouts[$(this).index()] = setTimeout(function(){
$("#"+id).stop(true).animate({"opacity":"1"},1000);
}, 100);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="label" id="dummyId1">Demo1</div>
<div class="label" id="dummyId2">Demo2</div>
<div class="label" id="dummyId3">Demo3</div>
<div class="label" id="dummyId4">Demo4</div>



<div class="imagenesC" id="imagenesC1">imagenesC1</div>
<div class="imagenesC" id="imagenesC2">imagenesC2</div>
<div class="imagenesC" id="imagenesC3">imagenesC3</div>
<div class="imagenesC" id="imagenesC4">imagenesC4</div>

关于javascript - 动态设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39631743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com