简体   繁体   English

如何为下一个对话框重置jQuery对话框超时?

[英]How to reset jquery dialog timeout for next dialog?

I have an add-to-bag button used throughout our site and we want a dynamic popup to appear to acknowledge what was just added, and then it goes away. 我在整个网站上都有一个“添加到购物袋”按钮,我们希望出现一个动态弹出窗口以确认刚刚添加的内容,然后它消失了。 I'm finding that if you click another add button, it has the previous dialog's timeout attached. 我发现,如果您单击另一个添加按钮,它将附加上一个对话框的超时。 To fix this so the next dialog has its own 10,000 setTimeout rather than whatever is left over from the last one I have come up with the following code (that doesn't do the trick). 为了解决这个问题,以便下一个对话框具有自己的10,000 setTimeout,而不是上一个对话框剩下的任何内容,我提供了以下代码(不起作用)。

$(document).ready(function ()
{
    // Create object for future dialog box - so it's available to the close method
    var addToBagDialogVar = $('<div id="addToBagDialogBox"></div>');
    var autoCloseTimeout = 10000;
    var dialogTimer;

    $(".addToBagPU").click(function (e)
    {
        var result = "";
        $.get(this.href, function (data) { SetData(addToBagDialogVar, data); });
        return false;
    });

    // Start listening for the close link click
    $(document).on("click", "#bagPUCloseLink", function (event)
    {
        event.preventDefault();
        CloseDialog(addToBagDialogVar);
    });

    function SetData(addToBagDialogVar, data)
    {
        result = data;
        var regex = data.match("{{(.*)}}");
        var bagCount = regex[1];

        addToBagDialogVar.html(result).dialog({
            open: function ()
            {
                clearTimeout(dialogTimer);
                $(".ui-dialog-titlebar-close").hide();
                SetBagCount(bagCount),
                dialogTimer = setTimeout(function () { CloseDialog(addToBagDialogVar); }, autoCloseTimeout);
            },
            show: { effect: "fadeIn", duration: 800 },
            close: function () { clearTimeout(dialogTimer); },
            width: 320
        });
    }

    function CloseDialog(closeThisDialog)
    {
        closeThisDialog.dialog("close");
        closeThisDialog.dialog("destroy").remove();
    }
});

The dialog is loaded with dynamic content from an external .Net page with product data and has a close link inside that page, which is why the dialog is loaded into addToBagDialogVar so it's available to CloseDialog . 该对话框从外部.Net页面加载了带有产品数据的动态内容,并且该页面内具有一个紧密链接,这就是为什么将该对话框加载到addToBagDialogVar中以便对CloseDialog可用的原因

All of that works just fine. 所有这些都很好。 It's just the reset of the timer that doesn't appear to be happening. 只是似乎没有发生计时器的重置。 If I go down a page of products and add each one to my bag, the 3rd or 4th dialog is only up for a second or so because they have all been using the first dialogs setTimeout. 如果我浏览产品页面并将每个产品添加到我的购物袋中,则第3或第4对话框只会显示一秒钟左右,因为它们都使用了第一个对话框setTimeout。

I've read and read and tried too many different ways to remember and now my brain is mush. 我已经阅读,阅读并尝试了太多不同的方式来记住,现在我的大脑变得糊涂了。

I propose an alternate explanation for the behavior you're observing. 我对您观察到的行为提出了另一种解释。 When you click the first "add to cart", a timer is started. 当您单击第一个“添加到购物车”时,将启动一个计时器。 As you go down the page clicking "add to cart", a new timer is started each time. 当您在页面上单击“添加到购物车”时,每次都会启动一个新计时器。 There's no overlap, just a bunch of separate timers running normally (although incidentally, each new dialog box blows away the timer ID you've previously created; I'll come back to this). 没有重叠,只有一堆单独的计时器正常运行(尽管顺便说一句,每个新对话框都会吹走您先前创建的计时器ID;我将返回到此)。

When your first dialog's timer expires, the dialog closes itself via the HTML ID, meaning it closes itself with something like a jquery $('div#addToBagDialogBox').closeOrSomethingLikeThat() , that is, every dialog inside a div with an id of addToBagDialogBox . 当第一个对话框的计时器到期时,该对话框会通过HTML ID自行关闭,这意味着它会使用类似jquery $('div#addToBagDialogBox').closeOrSomethingLikeThat()关闭自身,即div中ID为的每个对话框addToBagDialogBox The first timer expiration is closing all of your dialogs, because they all use that same HTML ID. 第一次计时器到期将关闭所有对话框,因为它们都使用相同的HTML ID。 The other timers are running perfectly, but when they expire there's nothing left for them to do. 其他计时器运行良好,但是当它们到期时,它们什么也没做。

You can fix the early-close problem by assigning a unique HTML ID to each dialog you create. 您可以通过为创建的每个对话框分配唯一的HTML ID来解决提前关闭问题。 And you'll want to manage your timer IDs on a per-dialog basis as well, such that each dialog has its own timer ID. 而且,您还希望基于每个对话框管理计时器ID,以便每个对话框都有自己的计时器ID。

Edit: Just for nerdy grins, think about the details of the scenario you described. 编辑:仅出于讨厌的笑容,请考虑您所描述的方案的详细信息。 Your first timer is running, counting down normally, and you start four other timers while the first dialog is still there. 您的第一个计时器正在运行,正常进行倒计时,而第一个对话框仍在其中时,您将启动另外四个计时器。 The ID of the fifth timer is in your variable dialogTimer . 第五个计时器的ID在您的变量dialogTimer So when the first dialog's timer expires, the close processing occurs, and you call clearTimeout with the ID of the fifth dialog's timer. 因此,当第一个对话框的计时器到期时,将进行close处理,并使用第五个对话框的计时器的ID调用clearTimeout So your first dialog's timer expired, the dialog closed all the other dialogs, and the cleanup cancelled the fifth timer. 因此,第一个对话框的计时器已到期,该对话框关闭了所有其他对话框,并且清除操作取消了第五个计时器。 There are three other timers still running, their IDs lost forever. 另外三个计时器仍在运行,其ID永远丢失。 They finally expire and their shutdown functions run, but they're totally without effect, their companion dialogs long gone. 它们最终到期,并且关闭功能运行,但是它们完全无效,伴随的对话框早已消失。 Sorry, bona fide nerd here. 对不起,善良的书呆子在这里。

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

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