JavaScript全局变量

时间:2011-07-11 10:04:11

标签: javascript

var channelsArray = [];
var percentArray= [];
var valueArray= [];
var jsonData,canvas,context;
var colorArray=["#ECD078","#D95B43","#C02942","#542437","#53777A"];
var posX=220;
var posY=60;
var width=55;
var graph=false;


//Webservice Request and Response begins....................................... 

        $.ajax({
            type: "POST",
            url: "http://localhost/WebSite1/myservice.asmx/GetData",
            dataType: "json",
           contentType: "application/json; charset=utf-8", 
            success: function(msg) {
        if(localStorage.getItem("channels")==null){
                  jsonData =jQuery.parseJSON(msg.d);
                  jsonToLocal();        
             }     
                 var strDisplay = " <table cellspacing='0' id='myTable' " +
                                 " <tr>" +
                                 " <th > Channels </th>" +
                                 " <th> Percentage</th>" +
                                 " <th> Value</th>" +
                                 " </tr>";
                for (var i = 0; i < colorArray.length; i++) {
                    strDisplay = strDisplay +
                                 " <tr style='cursor:pointer;' onclick='javascript:rotateChart("+i+")'>" +
                                 " <td>" + channelsArray[i] + "</a> </td>" +
                                 " <td> " + percentArray[i] + " </td>" +
                                 " <td> " + valueArray[i] + " </td>" +

                                 " </tr>";

                }
                strDisplay = strDisplay + "</table>";

                document.getElementById('DynamicGridLoading').innerHTML = strDisplay;
                  document.getElementById('myTable').setAttribute("class","datatable");


            },


             error:function(xhr, ajaxOptions, thrownError){
             alert(xhr.statusText);
            }
        });

//Webservice Request and Response ends........................

//converting json data into local storage....

function jsonToLocal(){




            for(i=0;i<jsonData.Table.length;i++){

               percentArray.push(jsonData.Table[i].toString());
               channelsArray.push(jsonData.Table[i].Channels);
               valueArray.push(jsonData.Table[i].Value);
            }


            try {
                    localStorage.setItem("percentage", percentArray.join(",")); 
                    localStorage.setItem("channels",channelsArray .join(","));
                    localStorage.setItem("value",valueArray.join(","));



                }
            catch (e) {
                            if (e == QUOTA_EXCEEDED_ERR) {
                                 alert("Quota exceeded!");
                             }
                      }

}

这是我几乎整个代码......我在percentArray函数jsontolocal中得到这个'未被捕获'的错误....如果我删除这一行..其他两个工作正常..所有这些数组都在推内部的字符串值..

2 个答案:

答案 0 :(得分:0)

你有一个错字:

 myfucntion()

应该是

 myfunction();

此代码有效(我将函数调用放在函数下面,因为否则在全局范围内(我在firebug控制台中测试)函数无法识别):

var myArray=[];


function myfunction(){

   myArray.push('s');   //at this point..my global array is not recognized.

}
myfunction();


alert(myArray[0]);//alert s

小提琴http://jsfiddle.net/GrpJw/1/

答案 1 :(得分:0)

我的问题只是随机和未知的javascript行为..为了确保这种情况永远不会发生..我发现在编写javascript代码时会遵循以下内容 - :

  1. 使用===与空值和0
  2. 进行比较
  3. 在使用parseInt..ie..parseInt(string,radix)时提供基数;
  4. 总是声明变量,例如for(var i = 0; ...而不是for(i = 0; ........
  5. 这些是我正在制造的错误......有时它会运行......有时它会显示随机行为..一直......我的问题解决了