Reputation: 1977
var cnt1 = 0;
function initOctoView(){
var newcnt1 = printAdsBox1(cnt1, imgPerBox1); // first time on first load
var totalBoxes8 = setInterval(function() {
newcnt1 = printAdsBox1(newcnt1, imgPerBox1); // all 5 sec
}, 5000);
}
This function get called by this:
if($('.octoView').length > 0){
initOctoView();
}
And works fine so far.
Later on I have:
$(document).on('click', 'a.windowXL', function () {
window.clearInterval(totalBoxes8);
}
But this returns that totalBoxes8 is not defined. What is my mistake? Please advice!
Upvotes: 0
Views: 380
Reputation: 6346
totalBoxes8
is undefined
because it is declared locally within the scope of the function initCotoView()
, and is thus unavailable to the global scope
you can declare a global from within the function by explicitly attaching it to the global window
object. Something like:
function foo() {
window.myVar = 1; // declares a global
}
foo(); // call the function to actually make the declaration
console.log(window.myVar); // the variable is accessible globally
Upvotes: 0
Reputation: 1959
You declare totalBoxes8 with var inside function - totalBoxes8 is local variable accesable in this function only. You may make it global:
var cnt1 = 0;
var totalBoxes8;
function initOctoView(){
var newcnt1 = printAdsBox1(cnt1, imgPerBox1); // first time on first load
totalBoxes8 = setInterval(function() {
newcnt1 = printAdsBox1(newcnt1, imgPerBox1); // all 5 sec
}, 5000);
}
Upvotes: 4
Reputation: 23208
Try this;
$(function(){
var cnt1 = 0, totalBoxes8 ;
function initOctoView(){
var newcnt1 = printAdsBox1(cnt1, imgPerBox1); // first time on first load
totalBoxes8 = setInterval(function() {
newcnt1 = printAdsBox1(newcnt1, imgPerBox1); // all 5 sec
}, 5000);
}
$(document).on('click', 'a.windowXL', function () {
window.clearInterval(totalBoxes8);
}
});
Upvotes: 0