setTimeout callback argument

前端 未结 4 1579
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 22:20

Let\'s consider this piece of JavaScript:

function Person(name) {
    this.name = name;
}

Person.prototype.showName = function() {
    alert(this.name);
}

         


        
4条回答
  •  离开以前
    2020-11-29 23:03

    setTimeout(mike.showName(), 5000); executes mike.showName() immediately and passes the return value to setTimeout()

    setTimeout(function(){ mike.showName(); }, 5000); passes a pointer to the function instead. That way setTimeout can execute the function, rather than it's return value.

提交回复
热议问题