TypeScript - 什么类型是f.e.的setInterval

问题描述 投票:4回答:3

如果我想将一个类型分配给一个变量,稍后将为其分配一个setInterval,如下所示:

this.autoSaveInterval = setInterval(function(){
      if(this.car.id){
        this.save();
      }
      else{
        this.create();
      }
    }.bind(this), 50000);

应该为this.autosave Interval变量分配什么类型?

javascript angular typescript typescript2.0
3个回答
8
投票

类型是数字;

private autoSaveInterval: number = setInterval( ()=>{console.log('123')},5000);

8
投票

类型取决于您将使用哪个函数,有2个重载,返回类型标记为红色边界框:

enter image description here

要使用返回号码的号码,请使用:

window.setInterval(...)

1
投票

使用typeof运算符查找任何变量的数据类型,如下所示:

typeof是一个一元运算符,放在一个可以是任何类型的操作数之前。它的值是一个字符串,它指定操作数的类型。

var variable1 = "Hello";
var autoSaveInterval;

this.autoSaveInterval = setInterval(function(){
      if(this.car.id){
        this.save();
      }
      else{
        this.create();
      }
    }.bind(this), 50000);
    
console.log("1st: " + typeof(variable1))
console.log("2nd: " + typeof(autoSaveInterval ))
© www.soinside.com 2019 - 2024. All rights reserved.