0

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);
});
user3253748
  • 89
  • 1
  • 13

2 Answers2

2

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);
});
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
  • @user3253748: Your Welcome. you could accept answer as a correct answer !! :) Because it can help may other users to find a solution. – Ishan Jain Jun 11 '14 at 08:36
  • No Problem If you already did this thing. But the "accept answer as a correct answer" means click on the check mark beside the answer to toggle it from hollow to green. See http://stackoverflow.com/help/accepted-answer And if you already did this thing then it's a bug on SO :) – Ishan Jain Jun 11 '14 at 08:45
  • O.K. indeed! :) fixed – user3253748 Jun 12 '14 at 16:43
1

Initialise the variable interval as global variable i.e. global scope, so that it can be accessed everywhere.

//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);
});
Community
  • 1
  • 1
Shaunak D
  • 20,588
  • 10
  • 46
  • 79