Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用承诺获取bind()的正确上下文_Javascript_Ajax_Oop_Requirejs_Promise - Fatal编程技术网

Javascript 无法使用承诺获取bind()的正确上下文

Javascript 无法使用承诺获取bind()的正确上下文,javascript,ajax,oop,requirejs,promise,Javascript,Ajax,Oop,Requirejs,Promise,我无法使用bind()在一个内部函数中获取正确的上下文。这是我的密码: return { /** * Checks if the table loader is active * @return {home} Returns this module for chaining */ loadStatusLog: function() { if ($("#status-log-box").length) { $.g

我无法使用
bind()
在一个内部函数中获取正确的上下文。这是我的密码:

return {
    /**
     * Checks if the table loader is active
     * @return {home} Returns this module for chaining
     */
    loadStatusLog: function() {
        if ($("#status-log-box").length) {
            $.getJSON("/Home/CurrentEvents", function(json, status, xhr) {
                if (status === "error") {
                    var errmsg = "Sorry but there was an error: ";
                    $("#result-msg").html(errmsg + xhr.status + " " + xhr.statusText);
                }
                else if (status === "success") {
                    if (json.globalGenerationStatus) {
                        console.log(this);
                        this.updateProgressInterval = setInterval(this.updateGenerationProgress, 1000);
                    }
                    var html = statusLog(json);
                    $("#status-log-table").html(html);
                }
            }.bind(this));
        }
        return this;
    },
    /**
     * Looks at the generation progress file and updates the progress bar for a running event
     * @returns {home} Returns this module for chaining
     */
    updateGenerationProgress: function() {
        var runningEvent = $(".running-event");
        $.ajax({
            type: "POST",
            url: "/Home/readGenerationStatusFile",
            global: false
        }).done(function(data) {
            runningEvent.css("width", data + "%").attr("aria-valuenow", data);
            if (data === "100") {
                console.log(this);
                clearInterval(this.updateProgressInterval);
                $("span", runningEvent).text("Complete").parent().removeClass("running-event").parent().removeClass("active progress-striped");
            }
        }.bind(this));
        return this;
    }

};

我的主要目标是从
updateGenerationProgress()
函数中清除
loadStatusLog()函数中设置的间隔。
loadStatusLog()
中的
console.log()
语句输出正确的上下文(此对象),但
updateGenerationProgress()中的
console.log()
语句作为上下文输出窗口。为什么会这样?关于承诺和上下文,我有什么遗漏吗?

您也需要在间隔上设置上下文

this.updateProgressInterval = 
    setInterval(this.updateGenerationProgress.bind(this), 1000);

否则,在非严格模式下,那里的上下文将设置为
窗口(或工作者),在严格模式下设置为
未定义的

哇,真不敢相信我忽略了这一点。非常感谢你!这解决了所有问题。@Aaron我很高兴能帮上忙,就我个人而言,我将从服务器获取内容的逻辑与更新DOM的逻辑分开。