2013-05-17 79 views
0
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); 
} 

此功能通過這個被調用:setInterval的變量不可訪問,

if($('.octoView').length > 0){ 
    initOctoView(); 
} 

而且能正常工作至今。

後來我:

$(document).on('click', 'a.windowXL', function() { 
    window.clearInterval(totalBoxes8); 
} 

但這返回totalBoxes8沒有定義。 我的錯誤是什麼?請指教!

+0

您呼叫totalBoxes8超出其範圍。 – adamb

+0

@adamb說的是什麼。超出範圍。你必須把它從功能中解放出來。 – RaphaelDDL

+0

嗨,好吧,我得到了範圍的問題。但即使我聲明它爲gloabl,它仍然不工作... 一些更多的細節:我打電話給initOctoView();從我的索引和我加載另一個頁面到iframe中。在這個iframe中,我有按鈕a.windowXL調用應該做clearInterval的函數。這可能是我使用的2個「幀」之間的問題嗎? – user1555112

回答

4

您聲明totalBoxes8與var內部函數 - totalBoxes8是局部變量只能在此函數中可訪問。您可以將其設置爲全球:

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); 
} 
0

試試這個;

$(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); 
    } 
}); 
0

totalBoxes8undefined,因爲它的功能initCotoView()的範圍內聲明本地,因而是不可用的全球範圍

你可以在功能從聲明全局通過明確地將其附加到全局window對象。類似於:

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