Global Javascript variables in Webkit
I have Chrome 7.0 and am trying to access fields of a global object. The code is working perfectly in Firefox and IE, but Chrome's debugger isn't helping me get anywhere. I tried Safari and it's also having trouble.
I can get the value of timer, but when I access status through the console I get "[object Object]"
. status.completedJobs
returns undefined
even after status = $.parseJSON(msg.d);
(json string is valid).
I'm not sure what to do after th开发者_如何学JAVAis. Calling $.parseJSON(msg.d);
from console works and I'm able to view the object's fields with the debugger. How do I get the status Object properly assigned and globally accessible?
Here's my code:
//Object that holds result of AJAX request
var status = new Object();
//Refresh timer variables
var timer;
var timer_is_on = 0;
$(document).ready(function() {
update();
doTimer();
});
/**
* Performs the AJAX request and updates the page
*/
function update() {
$.ajax({
type: "POST",
url: "Default.aspx/getStatus",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if (msg) {
try {
status = $.parseJSON(msg.d);
} catch (e) {
console.log(e);
}
updateProgressBar();
}
}
});
}
function updateProgressBar() {
var percent = Math.round(status.completedJobs / status.numJobs * 100);
$('#progressPercentage').text(percent + '%');
$('#progressbar').progressbar({
value: percent
});
}
/**
* Used to initialize the timer.
*/
function doTimer() {
if (!timer_is_on) {
timer_is_on = 1;
timedCount();
}
}
/**
* Executed on every time interval.
*/
function timedCount() {
update();
timer = setTimeout("timedCount()", 3000);
}
Try using another name than status, there is a predefined member of window(window is the global object in browser-based JS) called "status". It also would be good if you assign the global variable to the window-object to avoid conflicts if there exists a variable in the current (not global) scope with the same name:
window['statusVar'] = $.parseJSON(msg.d);
精彩评论