qappleh/Interview

第191题(2020-04-13):实现隔一段时间输出name?

Opened this issue · 2 comments

function A(name) {
  this.name = name;
}
A.prototype.log = function() {
    //每隔2s输出一下name
}
function A(name) {
  this.name = name;
}
A.prototype.log = function() {
  //每隔2s输出一下name
  if (this._interval) {
    clearInterval(this._interval)
  }
  this._interval = setInterval(_ => console.log(this.name), 2000)
}
function A(name) {
  this.name = name;
}
A.prototype.log = function() {
  //每隔2s输出一下name
  this._repeat = function(){
    let timeout = setTimeout(()=>{
        clearTimeout(timeout);
        console.log(this.name);
        this._repeat()
    }, 2000);
 }
 this._repeat();
}