1. Сценарии использования
В соответствии с положением рамки лица, нажатой ws, рисуем ее на канвасе, если сообщение не получено через 500 мс, очищаем канвас. Скриншот ниже:
Два, код выглядит следующим образом
/**
* timeTotal number 500ms 倒计时
* interval number 计时器间隔 100ms 每100ms执行一次
*/
listenNoMsg(timeTotal, interval) {
const TIME_COUNT = timeTotal;
let count = TIME_COUNT;
if (this.detectTimer) this.resetDetectTimer();
this.detectTimer = setInterval(() => {
if (count > 0 && count <= TIME_COUNT) {
count -= interval;
} else {
this.rectCtx.clearRect(0, 0, this.width, this.height);
this.resetDetectTimer();
}
}, interval);
},
//清理定时器
resetDetectTimer() {
clearInterval(this.detectTimer);
this.detectTimer = null;
},
Вызов выглядит следующим образом:
this.listenNoMsg(500, 100);