特定元素的角度设置计时器

时间:2019-04-07 02:33:41

标签: angular typescript angular7

这里我有一些jspdf函数

  public captureScreen() {
    // Start from here 
    this.loading = true;

    const data = document.getElementById('convert');
      html2canvas(data).then(canvas => {
      const imgWidth = 208;
      const pageHeight = 295;
      const imgHeight = canvas.height * imgWidth / canvas.width;
      const heightLeft = imgHeight;
      const contentDataURL = canvas.toDataURL('image/png');
      const pdf = new jspdf('p', 'mm', 'a4');
      const position = 0;
      pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
      pdf.save('Test.pdf');
      // Until here
      this.loading = false;
    });
  }

我想让此函数运行一秒钟,然后再将this.loading变回false。如何制作一个可以设置大约2-3秒的计时器,然后它才能执行最后一个代码?

1 个答案:

答案 0 :(得分:0)

使用设置超时功能使延迟几秒钟

// When calling function
this.loading = true;
setTimeout(function() { captureScreen(); }, 3000);

public captureScreen() {
    const data = document.getElementById('convert');
      html2canvas(data).then(canvas => {
      const imgWidth = 208;
      const pageHeight = 295;
      const imgHeight = canvas.height * imgWidth / canvas.width;
      const heightLeft = imgHeight;
      const contentDataURL = canvas.toDataURL('image/png');
      const pdf = new jspdf('p', 'mm', 'a4');
      const position = 0;
      pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);
      pdf.save('Test.pdf');
      // Until here
      this.loading = false;
    });
}