2010-08-27 21 views
2
window.onload = init; 

function init(){ 
    initializeEventHandlers(); 
    getData(formatDate(new Date)); 
    gotoDate('today'); 
} 

//scripts that manage the UI for tracker.aspx 
var dCurrentDate, sCurrentDate, sCurrentDayData, stepsVal, chipsVal, dayValue, dateValue, caloriesVal; 
var fCurrentValue = 0; 
var bAnimating, bSliding = false; 

    //scripts that manage the UI for tracker.aspx 
    var dCurrentDate, sCurrentDate, sCurrentDayData, stepsVal, chipsVal, dayValue, dateValue, caloriesVal; 
    var fCurrentValue = 0; 
    var bAnimating, bSliding = false; 

getData(d) 
{ 
    steps = 5; 
    calories = 10; 
    chipsAmount = 3; 
} 


//I expect this to be 5+1, but it actually through an exception. I am wondering if its cause steps is not yet loaded. What can I do to make this actually 5 + 1. 
steps + 1; 

我從XML中加載設置了某些值的數據。我正面臨的問題是,當我在GetData方法中設置值時。 onload後宣佈的增值稅將失去價值。我在做什麼錯了,我該如何使值保留在onload中調用某個方法時的值重置

+0

您是否異步加載數據?如果是這樣,這些值可能在您嘗試設置它們時不存在,所以您應該與腳本同步加載它。否則我不確定我完全理解你的問題。 – 2010-08-27 03:08:04

+0

是的,我同步加載 – ferronrsmith 2010-08-27 04:42:05

+0

這個問題主要是我需要在onload上設置變量,以便其他函數可以使用它們 – ferronrsmith 2010-08-27 04:47:14

回答

0

您正在加載數據的變量是否定義在全局範圍內?從您的getData()函數中,如果您將數據設置爲步驟,卡路里和chipsAmount變量,則它們將無法在該函數之外訪問。您必須在函數外部定義這三個變量,以便它們處於全局範圍內。

+0

未使用var聲明的函數內使用的變量由全局對象(即窗口)擁有並且是確實可以在其他地方訪問,而不管它們是否已全局聲明 – 2010-08-27 05:03:06

+0

@scott您是對的,我的一個疏忽 – Daniel 2010-08-27 05:29:30

相關問題