2011-07-14 65 views
0

感謝Sean的幫助。按照您的建議,我已將計算代碼移至其自己的方法。調試後,我意識到這個問題與全局存儲值有關。我認爲我意外創建了關閉。我已將計算代碼移入名爲calculate()的方法,該方法運行一次,以便全局設置變量的值。但是當方法運行時,設置了正確的值,只要方法退出並調用move()方法,所有值都設置爲undefined或0.在calculate方法中,我甚至使用隱藏字段來設置值('mills'),以便我可以將間隔時間傳遞給此testTimer = window.setInterval(move,document.getElementById('mills')。value,「testTimer」); thecalculate的代碼()`方法是這樣的: -JavaScript在setInterval之前未更新

var duration_equiv_seconds = 0; 
var duration_equiv_millsec = 0; 
var elapsed_percentage = 0; 
var elapsed = 0; 
var duration = 0; 

// function in one JS file 

function calculate() {  
    duration = document.getElementById('callDuration').value;  
    elapsed = document.getElementById('callElapsed').value;  
    duration_equiv_seconds = duration/100;  
    duration_equiv_millsec = duration_equiv_seconds * 1000;  
    document.getElementById('mills').value = Number(duration)/100 * 1000 ; 
    elapsed_percentage = (elapsed/duration) * 100;  
    display_progress(elapsed_percentage);  
    var countCalc = 0;  
    document.getElementById('status_message').innerHTML =" CountCalc = " + countCalc + " duration = " +  duration + " elapsed =" + elapsed + " elapsed2Var =" + elapsed2Var +" duration_equiv_seconds = " +  duration_equiv_seconds + " duration_equiv_millsec=" + duration_equiv_millsec + "  
    elapsed_percentage=" + elapsed_percentage;  countCalc++;  return duration_equiv_millsec; } 

function voice_progress (time_interval){ 
    // outer timer 
    voiceTimer = window.setInterval(function(){ 

    handleDurationElapsedRequest() 

         // outside 


    function move(){ 
       var count = 0; 
       duration = document.getElementById('callDuration').value; 
       elapsed = document.getElementById('callElapsed').value; 
       duration_equiv_seconds = duration/100; 
       duration_equiv_millsec = duration_equiv_seconds * 1000; 
       elapsed = Number(elapsed) + Number(duration_equiv_seconds); 
       elapsed_percentage = (elapsed/duration) * 100; 
       document.getElementById('callElapsed').value = elapsed; 
       display_progress(elapsed_percentage); 
       move_bar(elapsed_percentage); 
       if (elapsed_percentage >= 100){ 
        terminate_timer(testTimer); 
       } 
       document.getElementById('status_message').innerHTML =" at the endAt the end" + count + " duration = " + duration + " elapsed =" + elapsed +" duration_equiv_seconds = " + duration_equiv_seconds + " duration_equiv_millsec=" + duration_equiv_millsec + " elapsed_percentage=" + elapsed_percentage; 
       count++; 
       return duration_equiv_millsec;  
        } 
        var sleepTime = calculate(); // sleep time returns 0 

       testTimer = window.setInterval(move,document.getElementById('mills').value ,"testTimer");  
    },time_interval,"voiceTimer"); // end of outer Timer (time_interval = 1500) called from <body onload="voice_progress(1500);"> 

    } 

    //.................................................................`enter code here` 

    // in another JS file 
    // XMLHttpObject is created called, server returns the correct values 
    function handleDurationElapsedRequest(){ 
     if(xmlHttpObject.readyState==4){ 
      if(xmlHttpObject.status==200){ 
       var resp = xmlHttpObject.responseText; 
       var respArray = resp.split("-"); 
       document.getElementById('callElapsed').value = respArray[0]; //value set here is 47 
       document.getElementById('callDuration').value = respArray[1]; // value set here is 240 
       document.getElementById('mills').value = respArray[1]/100 * 1000 ; 

      } 
     } 
    } 

回答

0

的問題是,你正在做你的回調內部的計算,所以,當你開始,所有的變量都仍爲0

將時間計算拉出到它自己的函數中,然後在調用setInterval之前調用它,並且您的值將以正確的數字開始。 (當然,請繼續在您的回撥中撥打電話。)

您還正在重新安排move內的setInterval呼叫。這可能是錯誤的,因爲它會在每個間隔被重新調用,所以現在你有2個函數在定時器上被調用。如果您使用setTimeout,那麼您可能需要重新安排時間,但不要使用setInterval

你也有兩次相同的計算 - JavaScript是真實的代碼,你應該這樣對待它;把它拉出來,你可以調用一個函數。

- 編輯 -

現在叫從move()calculate()所以你不要有相同的代碼在兩個地方。 ;-)

callDurationcallElapsed只設置爲handleDurationElapsedRequest,並且只有當請求完成時。這可能就是你爲什麼得到0的原因。你究竟想在這裏做什麼?整個頁面在哪裏?

+0

謝謝肖恩,我仍然有探頭。我修改了代碼。請參見。謝謝 –

0

感謝您的幫助。我已經解決了這個問題。問題是我在讀取值「callDuration」和「callElapsed」之前,它們已經從服務器中檢索出來之後才更新。我通過在執行讀取這些值的代碼之前設置超時1.5秒來解決這個問題,即window.setTimeout(「calculate()」,1500);

再次感謝您的幫助。