在查找事件完成时运行函数

我有这两个函数:


async takeScreenShot() {

  this.pauseVideo();

  if (this.animations.length && !this.ended) {

    this.pauseLotties()

  }

  this.captures.push(this.canvas.toDataURL('image/webp'));

  if (!this.ended) {

    setTimeout(() => {

      this.takeScreenShot();

    }, 500);

  }

},


async pauseVideo() {

  console.log("currentTime", this.video.currentTime);

  console.log("duration", this.video.duration);

  this.video.pause();

  const oneFrame = 1 / 30;

  if (this.video.currentTime + oneFrame < this.video.duration) {

    this.video.currentTime += oneFrame;

  } else {

    this.video.play()

  }

}

现在,我每500毫秒拍摄一次画布的屏幕截图。但我想使用seek事件截取屏幕截图,并承诺在完成搜索时让我知道,以便我可以截取屏幕截图。这样,它应该更有效地翻录视频,并可能更快。我该怎么做?setTimeout


开心每一天1111
浏览 80回答 2
2回答

慕的地8271018

takeScreenShot(){return new Promise((resolve,reject)=>{&nbsp; &nbsp; this.video.addEventListener("seeked", ()=>{&nbsp; &nbsp; &nbsp; &nbsp; return resolve()&nbsp; &nbsp; });})}并使用调用它&nbsp; &nbsp; this.takeScreenShot().then(()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.pauseVideo()&nbsp; &nbsp; &nbsp;}).then(()=>{console.log("Successfull completed")})请看我知道这是否有帮助

慕标5832272

这就是我想出的解决方案。虽然这并不完全是Sandeep Nagaraj所建议的,但他的评论确实对我找到解决方案有很大帮助。因此,我赞成他担任这一职务。async takeScreenShot(){&nbsp; let seekResolve;&nbsp; this.video.addEventListener("seeked", async () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (seekResolve) seekResolve();&nbsp; &nbsp; &nbsp; &nbsp; });await new Promise(async (resolve,reject)=>{&nbsp; console.log("promise running", this.video);&nbsp; if(!this.ended){&nbsp; if(this.animations.length){&nbsp; &nbsp; this.pauseLotties()&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;this.pauseVideo();&nbsp; await new Promise(r => (seekResolve = r));&nbsp; this.layer.draw();&nbsp; this.captures.push(this.canvas.toDataURL('image/webp'));&nbsp; resolve()&nbsp; this.takeScreenShot()&nbsp; &nbsp; }})},
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript