简体   繁体   English

我如何删除此setTimeout函数

[英]How can i remove this setTimeout function

I'm initiating a popup if the user is inactive on the page but want the function that shows the popup to be removed if the user closes the popup, as this is just annoying having this pop open every XX amount of seconds. 如果用户在页面上处于非活动状态,则我要启动一个弹出窗口,但希望在用户关闭弹出窗口时删除显示弹出窗口的功能,因为这很烦人此弹出窗口每隔XX秒打开一次。

This is the function I'm using to call the popup. 这是我用来调用弹出窗口的函数。

<script type="text/javascript">
var timeoutID;

function setup() {
    this.addEventListener("mousemove", resetTimer, false);
    this.addEventListener("mousedown", resetTimer, false);
    this.addEventListener("keypress", resetTimer, false);
    this.addEventListener("DOMMouseScroll", resetTimer, false);
    this.addEventListener("mousewheel", resetTimer, false);
    this.addEventListener("touchmove", resetTimer, false);
    this.addEventListener("MSPointerMove", resetTimer, false);

    startTimer();
}
setup();

function startTimer() {
    // wait 20 seconds before calling goInactive
    timeoutID = window.setTimeout(goInactive, 20000);
}

function resetTimer(e) {
    window.clearTimeout(timeoutID);

    goActive();
}


function goActive() {         
    startTimer();
}

function goInactive() {
    $.magnificPopup.open({
        mainClass: "mfp-fade",
        fixedContentPos: false,
        fixedBgPos: true,
        items: {
            src: "#needtochat"
        },
        removalDelay: 1000, //delay removal by X to allow out-animation
          callbacks: {
            beforeOpen: function() {
               this.st.mainClass = "mfp-3d-unfold";
            },
            close: function() {
                window.clearTimeout(timeoutID); // Thought this may remove the timer function all together?
            }
          },
        type: "inline"}, 0);
}

</script>

You'll notice the function goInactive() shows the modal window and I have a callback close: that I hoped would clear the function. 您会注意到function goInactive()显示了模式窗口,并且我有一个回调close:我希望可以清除该函数。

Assuming that the close callback works as advertised, the timeout will be being cleared. 假设close回调如广告中所述,超时将被清除。 However, you have attached several event listeners that will immediately recreate the timeout as soon as something happens. 但是,您已附加了几个事件侦听器,一旦发生事件,它们将立即重新创建超时。 There are two options here. 这里有两个选择。

Option 1: Remove the event listeners, in addition to clearing the timer: 选项1:除了清除计时器之外,还删除事件侦听器:

function teardown() {
    this.removeEventListener("mousemove", resetTimer);
    this.removeEventListener("mousedown", resetTimer);
    this.removeEventListener("keypress", resetTimer);
    this.removeEventListener("DOMMouseScroll", resetTimer);
    this.removeEventListener("mousewheel", resetTimer);
    this.removeEventListener("touchmove", resetTimer);
    this.removeEventListener("MSPointerMove", resetTimer);
}

and within the close handler: close处理程序中:

close: function() {
    teardown();
    window.clearTimeout(timeoutID);
}

Option 2: Create a flag and use that to control the creation of the timer: 选项2:创建一个标志并使用它来控制计时器的创建:

var popupClosed = false;

function resetTimer(e) {
    window.clearTimeout(timeoutID);
    if (!popupClosed)
        goActive();
}

And set that within the close handler: 并在close处理程序中进行设置:

close: function() {
    popupClosed = true;
    window.clearTimeout(timeoutID);
}

As I understand you can add a variable, for example: 据我了解,您可以添加一个变量,例如:

var popupBeenClosed = false;

change it to true in the "close" callback, and add a check in the resetTimer(): 在“关闭”回调中将其更改为true,并在resetTimer()中添加检查:

function resetTimer(e) {

    if (!popupBeenClosed) {

        window.clearTimeout(timeoutID);
        goActive();
    }

} }

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

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