「JavaScript」window.setTimeout()でbind関数を呼び出すサンプル

2021年2月9日

書式
window.setTimeout(this.declare.bind(this), 時間);
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function FuncA() {
this.valCount = Math.floor(Math.random() * 25) + 1;
}
FuncA.prototype.declare = function() {
console.log(`value is ${this.valCount}`);
};
// setTimeout()で2秒遅延させてから notice を宣言
FuncA.prototype.notice = function() {
window.setTimeout(this.declare.bind(this), 2000);
};
const cft = new FuncA();
cft.notice();
function FuncA() { this.valCount = Math.floor(Math.random() * 25) + 1; } FuncA.prototype.declare = function() { console.log(`value is ${this.valCount}`); }; // setTimeout()で2秒遅延させてから notice を宣言 FuncA.prototype.notice = function() { window.setTimeout(this.declare.bind(this), 2000); }; const cft = new FuncA(); cft.notice();
function FuncA() {
  this.valCount = Math.floor(Math.random() * 25) + 1;
}

FuncA.prototype.declare = function() {
  console.log(`value is  ${this.valCount}`);
};

// setTimeout()で2秒遅延させてから notice を宣言
FuncA.prototype.notice = function() {
  window.setTimeout(this.declare.bind(this), 2000);
};
const cft = new FuncA();
cft.notice();

実行結果
> “value is 20"

JavaScript

Posted by arkgame