can't clearInterval on modal window close action

I'm using modal windows from Foundation and I have an alert every 3 seconds when it's opened. The point is I want to disable the interval when modal window is closed. I tried the following but clearInterval function doesn't seem to work (alert is still genarated after modal is closed):

$(document).on('opened', '[data-reveal]', function () {
            var interval = setInterval(function(){alert("O.K.");}, 3000);
        if($("div#automate_message").height() > 100) { interval; }
});
        $(document).on('close', '[data-reveal]', function () {
        clearInterval(interval);
});

In your code the variable interval is out of the scope when you are trying to clearInterval .

So, First declare the variable interval globally.

Globally declared variables called : GLOBAL VARIABLES - its value is accessible and modifiable throughout the program everywhere.

try this:

var interval = null;
$(document).on('opened', '[data-reveal]', function () {
    interval = setInterval(function(){alert("O.K.");}, 3000);
    if($("div#automate_message").height() > 100) { interval; }
});

$(document).on('close', '[data-reveal]', function () {
    clearInterval(interval);
});

将变量interval初始化为全局变量,即全局范围,以便可以随处访问。

//global scope
var interval = null;

$(document).on('opened', '[data-reveal]', function () {

        //local scope

        interval = setInterval(function(){alert("O.K.");}, 3000);
        if($("div#automate_message").height() > 100) { interval; }
});

$(document).on('close', '[data-reveal]', function () {
        clearInterval(interval);
});
链接地址: http://www.djcxy.com/p/95040.html

上一篇: 如何在cordova应用程序中为ajax调用配置服务器主机IP地址和端口

下一篇: 模式窗口关闭操作无法清除Interval