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

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

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

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

它调用这个函数:

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

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

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

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

mzsu5hc0

mzsu5hc01#

将代码从:

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

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

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

展开查看全部
g6ll5ycj

g6ll5ycj2#

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

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

相关问题