backbone.js 控制台日志消息未显示在浏览器控制台中

yvgpqqbh  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(226)

我在我的Backbone视图中有一个函数,用于通过后端的API创建一个新对象:

//***called when remote hardware signal triggered
createConference: function () {
    var self = this;
    console.log("ScheduleLocationArea.js - createConference() ")
    const startTime = performance.now();
    this.sysLocation.create().then((response) => {
        self.model.collection.add(response);
    });
    const duration = performance.now() - startTime;
    console.log(`PERFORMANACE CHECK: ScheduleLocationArea.js - the function createConference() took ${duration}ms`);
},

它调用这个函数:

// called from within createConference
async create() {
    console.log("Location.js - create() ")
    const startTime = performance.now();
    return await this.sync('create', this, {
        url: this.create.url(this.id)
    }, { silent: true });
    const duration = performance.now() - startTime;
    console.log(`PERFORMANACE CHECK: Location.js - the function create() took ${duration}ms`);
},

如您所见,我正在尝试检查性能问题。
但是由于某种我不知道的原因,它没有完成create()函数。我从来没有看到这个函数的PERFORMANACE CHECK
以下是我的控制台输出:

ScheduleLocationArea.js - createConference() 
Location.js:22 Location.js - create() 
ScheduleLocationArea.js:269 PERFORMANACE CHECK: ScheduleLocationArea.js - the function createConference() took 1.7000000476837158ms

浏览器很快就写出了上面所有的控制台消息。
尽管它说只花了1. 7毫秒,但实际上只花了3秒钟。
所以我不明白为什么要花这么长时间,为什么它不写出create()函数的性能数据。
我做错什么了吗?
谢谢你!

mzsu5hc0

mzsu5hc01#

将代码从:

// called from within createConference
async create() {
    console.log("Location.js - create() ")
    const startTime = performance.now();
    return await this.sync('create', this, {
        url: this.create.url(this.id)
    }, { silent: true });
    const duration = performance.now() - startTime;
    console.log(`PERFORMANACE CHECK: Location.js - the function create() took ${duration}ms`);
},

// called from within createConference
async create() {
    console.log("Location.js - create() ")
    const startTime = performance.now();
    const newUrl = await this.sync('create', this, {
        url: this.create.url(this.id)
    }, { silent: true });
    const duration = performance.now() - startTime;
    console.log(`PERFORMANACE CHECK: Location.js - the function create() took ${duration}ms`);
    return newUrl;
},

这将允许您的函数在返回创建的值之前显示性能日志。

g6ll5ycj

g6ll5ycj2#

在第一个代码片段中调用console.log之前,您正在从函数返回。return语句后面的任何代码都不会运行:

// called from within createConference
async create() {
    console.log("Location.js - create() ")
    const startTime = performance.now();
    return await this.sync('create', this, {
        url: this.create.url(this.id)
    }, { silent: true });
    // this following code never runs as screate(0 has returned already
    const duration = performance.now() - startTime;
    console.log(`PERFORMANACE CHECK: Location.js - the function create() took ${duration}ms`);
},

相关问题