Javascript jquery,如果用户单击一个div,是否超时另一个jquery函数?
我有下面的脚本,它在多个div中淡出,称为“noti_box”。如果用户关闭这些div,则另一个div“advert”会在其位置淡出Javascript jquery,如果用户单击一个div,是否超时另一个jquery函数?,javascript,jquery,Javascript,Jquery,我有下面的脚本,它在多个div中淡出,称为“noti_box”。如果用户关闭这些div,则另一个div“advert”会在其位置淡出 <script> $(document).ready(function(){ var animations = []; $('.noti_box').each(function(i) { animations.push( $(this).hide().delay(i * 1000)
<script>
$(document).ready(function(){
var animations = [];
$('.noti_box').each(function(i) {
animations.push(
$(this).hide().delay(i * 1000).fadeIn(1500).promise()
);
});
$.when.apply($, animations).done(function () {
time=setInterval(function(){
if ( $('.noti_box:visible').length === 0 ) {
$(".advert").fadeIn("slow");
} },200);
});
});
</script>
$(文档).ready(函数(){
var=[];
$('.noti_box')。每个(函数(i){
动画.推送(
$(this.hide().delay(i*1000).fadeIn(1500.promise())
);
});
$.when.apply($,动画).done(函数(){
时间=设置间隔(函数(){
如果($('.noti_框:可见')。长度==0){
$(“.advert”).fadeIn(“慢”);
} },200);
});
});
这很好,基本上这里发生的是我的最后一个函数被卡在一个循环上,当“noti_box”在页面上不可见时,“advert”div消失
但是,现在我希望用户单击名为“icons”的div,如果单击了,则应在“noti_box”div中重新淡入,并使用以下代码淡出“advert”div:
<script>
$('.icons').click(function(){
$('.advert').fadeOut('fast');
$('.noti_box).fadeIn('fast');
});
</script>
$('.icons')。单击(函数(){
$('.advert').fadeOut('fast');
$('.noti_box).fadeIn('fast');
});
我在这里遇到的问题是“advert”div在眨眼之间就会消失,而不会在我的“noti_box”div中消失。这是因为我的第一个javascript函数仍然在循环中,阻止了我的第二个脚本执行
因此,我需要做的是,当用户单击我的div‘icon’时,为我的第一个脚本设置一个超时间隔,然后在脚本执行并且再次显示‘noti_box’div后清除超时间隔
有人可以告诉我如何才能做到这一点,因为我是jquery的新手。谢谢
function notiBox(ele){
this.ele=ele;
this.ele.hide().fadeIn('slow');
console.log("I have been born! "+ele);
}
notiBox.prototype={
constructor:notiBox,
advert:function(){
var ele=this.ele;
this.ele.fadeOut('fast',function(){ele.next('.advert').fadeIn('slow');});
},
fadeBack:function(){
var ele=this.ele;
this.ele.next('.advert').fadeOut('slow',function(){ele.fadeIn('slow');});
},
}
$(document).ready(function(){
var timeIn=1;
$('.noti-box').each(function(){
var self=this;
this.timer=setInterval(function(){self.notiBox=new notiBox($(self));clearInterval(self.timer);},1000*timeIn);
timeIn++;
});
$('.icon').click(function(){
$('.noti-box').notiBox.fadeBack();
});
});
以上是一种基于“面向对象”的方法来解决您的问题。您可能遇到的唯一问题是,您的广告div不在box div的旁边。很抱歉,我猜您的DOM元素和布局。我的方法也不正确,因为我已经很久没有写过这样的东西了。我要做一些测试。同时,你能提供一些HTML吗?这样我就可以调整我的代码:d从多少个div开始调用noti box?我想让它们成为对象,而不是你在做什么。更简单的是,您只做了一次,而不是在for循环中。为什么要使用setInterval?这就是造成问题的原因:p@sourRaspberri没有设置数量的“noti_box”div,根据数据库中保存的记录,可以有1、2、3或4等。我之所以使用设置间隔,是因为我的“noti_盒”div会在一个小延迟后淡入,如果我没有设置间隔,则“advert”div会淡入,而“noti_盒”div仍会淡入,这样可以确保只有当用户关闭noti_盒divsOk时才会显示广告div,我正在写一些东西,但我已经很久没有做过这样的事情了,所以可能不是100%正确,但它会让你开始正确的道路:)@sourRaspberri我很感激,谢谢