开发者

JavaScript Global Variables

开发者 https://www.devze.com 2023-03-19 03:24 出处:网络
var channelsArray = []; var percentArray= []; var valueArray= []; var jsonData,canvas,context; var colorArray=[\"#ECD078\",\"#D95B43\",\"#C02942\",\"#542437\",\"#53777A\"];
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....

functi开发者_JAVA技巧on 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!");
                             }
                      }

}

this is my almost entire code...am getting this 'uncaught' error in function jsontolocal at percentArray....if i remove this line..other two work fine.. all these arrays are pushing string values inside dem..


You have a typo :

 myfucntion()

should be

 myfunction();

this code works (i put the function call beneath the function because otherwise in the global scope (i tested it in firebug console) the function wasn't recognized):

var myArray=[];


function myfunction(){

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

}
myfunction();


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

fiddle http://jsfiddle.net/GrpJw/1/


my problem was nothing but random and unknown behavior of javascript.. To ensure that this never happens..i found out to follow following things while writing a javascript code -:

  1. use === to compare with a null value and 0
  2. provide radix while using parseInt..ie..parseInt(string,radix);
  3. always declare variables like for(var i=0;...and not for(i=0;........

these were the mistakes i was making..sometimes it would run..sometimes it would show a random behavior..anyways..my problem is solved

0

精彩评论

暂无评论...
验证码 换一张
取 消