简体   繁体   English

我正在尝试在 Typescript 中创建一个简单的计时器,在浏览器控制台中它说“this.pad is not a function”,我不明白为什么

[英]I am trying to create a simple timer in Typescript, in the browser console it says "this.pad is not a function" and I don't get why

I am well aware this is not very Angular friendly, in fact I'd like to know from you a better fix to make it more "Angular-y" if possible, thank you.我很清楚这对 Angular 不是很友好,事实上我想从你那里知道一个更好的修复方法,如果可能的话,让它更“Angular-y”,谢谢。

export class TimerComponent implements OnInit {
  min = 0;
  sec = 0;
  minutesLabel: any = document.getElementById('minutes'); 
  secondsLabel: any = document.getElementById('seconds');

  startTimer() {
    const interval = setInterval(this.setTime, 1000); 
    return interval;
  }

  setTime() {
    ++this.sec;
    var r: any = this.sec / 60;
    this.secondsLabel.innerHTML = this.pad(this.sec % 60);
    this.minutesLabel.innerHTML = this.pad(parseInt(r));
  }

  pad(val: any) {
    var valString = val + '';
    if (valString.length < 2) {
      return '0' + valString;
    } else {
      return valString;
    }
  }

  constructor() {}

  ngOnInit() {}
}

You have a few solutions here:您在这里有一些解决方案:

setInterval(this. setTime.bind(this), 1000); 

or:或者:

setInterval(() => this.setTime(), 1000); 

The reason being that when you pass setTime into setTimeout , the this scope of your object instance isn't preserved.原因是当您将setTime传递到setTimeout ,不会保留对象实例的this范围。

Here is a answer that also makes it as you said "Angular-y".这是一个答案,正如您所说的“Angular-y”。

This makes use of rxjs timer which is similar to setTimeout but more configurable, form prod use you will need some way to unsubscribe on destroy.这利用了 rxjs 计时器,它类似于 setTimeout 但更可配置,表单 prod 使用您需要某种方式在销毁时取消订阅。

I also simplified a lot here that was not needed, as well as added better type definitions.我还在这里简化了很多不需要的东西,并添加了更好的类型定义。

import { Component, OnInit } from '@angular/core';
import { timer } from 'rxjs';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  minutesLabel: string;
  secondsLabel: string;

  ngOnInit() {
    timer(0, 1000).subscribe((sec) => {
      this.secondsLabel = this._pad(sec % 60);
      this.minutesLabel = this._pad(Math.floor(sec / 60));
    });
  }

  private _pad(val: number) {
    var valString = val + '';
    if (valString.length < 2) {
      return `0${valString}`;
    } else {
      return valString;
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不需要导出/导入TypeScript接口? - Why don't I need to export/import TypeScript Interfaces? 为什么我没有在模板中获得变量? - Why don't I get variable in template? 我试图理解这段TypeScript代码 - I am trying to understand this piece of TypeScript code 我的功能没有运行,我不知道为什么? (Angular 8)我没有错误 - My function is not running and I don't know why? (Angular 8) I get no errors 我正在尝试将元素从 object 推入两个 arrays。 在 Edge 浏览器的控制台上返回 undefined - I am trying to push element into two arrays from an object. Returned undefined on the console in the Edge browser 为什么不能从Typescript类的实例成员函数或方法中获取值? - Why can't I get a value from an instance member function or method from typescript class? 嘿,有没有办法嵌套 ngTemplateOutlets? 我正在尝试创建一个简单的可自定义模式 - Hey, so is there a way to nest ngTemplateOutlets? I am trying to create a simple customizable modal 我不明白,为什么我的 CalculateTotal () function 返回“Null”? - I don't understand, Why is my CalculateTotal () function returning "Null"? 依赖注入:我所学到的东西以及为什么我不明白 - Dependency injection: What I am taught and why I don't understand that 当我尝试运行我的 angular 项目时,我的浏览器上出现“无法访问此站点” - I am getting "This site can’t be reached" on my browser when I am trying to run my angular project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM