在onload中调用方法时重置值

时间:2010-08-27 02:37:04

标签: javascript xml ajax

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之后声明的vars失去了价值。我做错了什么,如何保留价值

1 个答案:

答案 0 :(得分:0)

您正在加载数据的变量是否已在全局范围内定义?从你的getData()函数,如果你将数据设置为steps,calories和chipsAmount变量,它们将无法在该函数之外访问。您必须在函数外部定义这三个变量,以便它们位于全局范围内。