2011-04-19 41 views
0

這個問題可能似乎是前面提到的類似問題,但它不是。當條件中包含警報時,setInterval可以工作,否則它只能工作一次

我的問題是以下幾點: 我的代碼片段如下:

var i = 0;    

    function func2() {     
    if (i==0){ 

    document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','channels'); 
    var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name'); 
    //alert("Attribute value set :" + strValue); 
    document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER); 
    i++; 

    } 
    else if(i==1){ 

    // For Channels section -- "Get current channel details" 
    document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','get'); 
    var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name'); 
    //alert("Attribute value set :" + strValue); 
    document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER); 
    i++; 

    } 
    else if(i==2){ 

    // For Channels section -- "Set current channel details" 
    document.getElementById('frame1').contentWindow.opts[selected].setAttribute('name','set'); 
    document.getElementById('frame1').contentWindow.menuSelect(1); 
    var strValue = document.getElementById('frame1').contentWindow.opts[selected].getAttribute('name'); 
    //alert("Attribute value set :" + strValue); 
    document.getElementById('frame1').contentWindow.handleKeyCode(VK_ENTER);  
    i++; 

    } 
    } 

    $(function() { 
     setInterval(function(){func2();}, 1000); 
    }); 

在這個setInterval的作品只有一次即只執行第一個if條件。但是,當我啓用所有條件的「警報」消息時,setInterval可以正常工作。

我不想爲這個setInterval的「警報消息」條件,它應該按照條件和給定的時間間隔執行每個if else語句。

我可以實現這一點。我也沒有找到任何令人滿意的參考從這個論壇setInterval相關的問題。請幫忙。

謝謝。

+0

*請*,不要大膽你的問題大塊。它很煩人。 – Blender 2011-04-19 04:45:30

+0

爲什麼你用jQuery標記這個問題,這是純Javascript,你爲什麼不試試用jQuery? – diEcho 2011-04-19 04:48:57

+0

這只是jquery,上面的代碼片段在$(document).ready(function(){var $ frame = $('#frame1'); $ frame.ready(function(){------- ------}); only。 – Kamakshi 2011-04-19 05:06:32

回答

1

我相信你在func2裏面得到了一個例外,這使得它看起來好像沒有運行多次。當它爲空時讀取.contenWindow。嘗試使用try/catch來捕獲異常。或者使用調試器。

+0

您好,感謝您寶貴的回覆,其實上面的代碼片段工作正常,當我添加警報消息每個條件如果你可以看到我目前評論,如果我取消註釋這些警報消息,那麼它的工作正常。但我不想要顯示警報消息,仍然應該工作相同。 – Kamakshi 2011-04-19 05:52:00