简体   繁体   English

jQuery fadeOut一遍又一遍执行回调,这是为什么,如何防止这种情况呢?

[英]Jquery fadeOut executes callback over and over again, why is this and how can I prevent this?

My method fades a div box out and has a callback that calls a method... like this: 我的方法淡出div框,并有一个调用方法的回调...像这样:

function closeWindow(windowIdPrefix, speed) {
    $("#" + windowIdPrefix + "_ViewPanel").fadeOut(speed,
        function() {
            resetWindow(windowIdPrefix);
            numWindowsOpen--;
        }
    );
}

function resetWindow(windowIdPrefix) {
   alert("resetting window");
}

When this executes (onClick of a button) I have an alert in resetWindow to see how many times it executes. 当它执行时(按钮的onClick),我在resetWindow中有一个警报,以查看它执行了多少次。

It seems to execute forever, but I haven't sat there long enough closing the alert windows to find out. 它似乎永远执行,但我还没有坐在那儿足够长的时间关闭警报窗口以进行查找。

I did some research and read in the Jquery Documentation: 我做了一些研究,并阅读了Jquery文档:

callback (Optional) Function 回调(可选)功能
A function to be executed whenever the animation completes, executes once for each element animated against . 每当动画完成时要执行的功能,针对动画的每个元素执行一次

So I was wondering, even though I'm only fading out 1 div, does it count as 1.. plus 1 for each child element the div has? 所以我想知道,即使我只是淡出1 div,它是否也算作1.。div的每个子元素加1?

Technically they are being animated because the inner elements are being faded out with the outer div, but if you watch the javascript in firebug only the outer div I'm fading out gets it's opacity/display changed. 从技术上讲,它们是动态的,因为内部元素随着外部div逐渐淡出,但是如果您在firebug中观看javascript,则只有我正在淡出的外部div的不透明度/显示发生了变化。

If this is what's happening, how do I make sure the callback only gets executed once? 如果发生这种情况,如何确保回调仅执行一次?

Edit: It was the line numWindowsOpen--; 编辑:这是行numWindowsOpen--; I hadn't defined numWindowsOpen before the function so for some reason that was making the call happen multiple times... Can anyone explain why this was happening? 我没有在函数之前定义numWindowsOpen,因此由于某种原因使调用多次发生...有人可以解释为什么发生这种情况吗?

The callback should only be invoked once for each element that matched the selector. 对于与选择器匹配的每个元素,回调仅应被调用一次。 Do you see multiple alerts when putting an alert message directly in the callback? 将警报消息直接放在回调中时,是否看到多个警报?

function() {
    alert('Test');
    resetWindow(windowIdPrefix);
    numWindowsOpen--;
}

问题是numWindowsOpen在它递减之前没有被声明。提醒您,在进行更复杂的处理之前,请确保您先查找类似的简单内容。

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

相关问题 淡出结束后,将执行jQuery淡出回调函数 - JQuery fadeOut callback function is being executed after fadeOut is over 我是否在此jQuery代码中一再绑定事件? - Am I binding events over and over again in this jQuery code? 如何防止jQuery-UI手风琴在鼠标悬停时选择错误的项目? - How can I prevent a jQuery-UI accordion from selecting the wrong item on mouse over? 如何使用JavaScript反复添加和删除元素? - How can I use javascript to add and remove an element over and over again? 鼠标悬停的fadein fadeout jquery - fadein fadeout jquery on mouse-over 如何遍历Java / JavaScript中的回调函数? - How can I loop over Callback functions in Java/JavaScript? 我如何制作动画5秒钟,然后淡出()jquery - How can I animate for 5 seconds and then fadeOut() jquery 我可以使用JavaScript来防止一遍又一遍地加载相同的脚本吗? - Can i use javascript to prevent Loading the same script over and over? 为什么要使用回调函数而不是在jQuery中触发事件? - Why use a callback function over triggering an event in jQuery? 我如何一次又一次地遍历数组,同时将其每个值分配给DOM中的所有svg - How can I iterate through an array over and over again while assigning each of its values to all the svgs in my DOM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM