gpt4 book ai didi

javascript - 为什么我的每个类使用的 jQuery 片段不支持 setTimeout?

转载 作者:行者123 更新时间:2023-12-03 08:07:39 25 4
gpt4 key购买 nike

以下代码由于某种原因无法工作,它不喜欢 setTimeout 部分,相反,我能做的唯一工作版本是没有延迟...

jQuery(".divhere").hide();
if(jQuery('.divhere').length >= 1){
jQuery(".divhere").each(function() {
setTimeout(function(el) {
jQuery(this).slideDown("slow");
jQuery(this).show();
}, 1000);
});
}

唯一有效的是:

jQuery(".divhere").hide();
if(jQuery('.divhere').length >= 1){
jQuery(".divhere").each(function() {
jQuery(this).slideDown("slow");
jQuery(this).show();
});
}

最佳答案

this 上下文在 setTimeout 场景中丢失。如果您尝试在 setTimeout 中使用 console.log(this),您将找到 window 对象。

Use .bind: JavaScript’s Bind Allows Us to Set the this Value on Methods

试试这个:

jQuery(".divhere").hide();
if(jQuery('.divhere').length >= 1){
jQuery(".divhere").each(function() {
setTimeout(function(el) {
jQuery(this).slideDown("slow");
jQuery(this).show();
}.bind(this), 1000);
});
}

关于javascript - 为什么我的每个类使用的 jQuery 片段不支持 setTimeout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34312268/

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