Webkit中的全局Javascript变量

che*_*ker 2 javascript safari webkit google-chrome global-variables

我有Chrome 7.0,我正在尝试访问全局对象的字段.代码在Firefox和IE中完美运行,但Chrome的调试器无法帮助我实现任何目标.我试过Safari,它也遇到了麻烦.

我可以获得计时器的值,但是当我通过控制台访问状态时,我得到了"[object Object]". status.completedJobs返回undefined即使在status = $.parseJSON(msg.d);(JSON字符串是有效的).

我不确定在此之后该怎么做.$.parseJSON(msg.d);从控制台调用工作,我可以使用调试器查看对象的字段.如何正确分配和全局访问状态对象?

这是我的代码:

//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);
}
Run Code Online (Sandbox Code Playgroud)

Dr.*_*lle 5

尝试使用除状态之外的其他名称,有一个预定义的窗口成员(窗口是基于浏览器的JS中的全局对象),称为"状态".如果将全局变量分配给window-object以避免冲突(如果当前(非全局)作用域中存在具有相同名称的变量,那么也会很好:

window['statusVar'] = $.parseJSON(msg.d);
Run Code Online (Sandbox Code Playgroud)