如果我理解正確你的要求,你需要檢查時,鼠標也離開元素爲好,然後使用和運營商像scan
總結總量。一個相對簡單的方式做,這將是獲得mouseover
的時間戳,然後品嚐的mouseout
事件:
var result = document.getElementById('result');
var mouseOver = Rx.Observable.fromEvent(result, 'mouseover');
var mouseOut = Rx.Observable.fromEvent(result, 'mouseout');
mouseOver
//Get the time of the mouseover event
.timestamp()
//Don't emit until the mouseOut triggers
.sample(mouseOut)
//Extract only the timestamp value
.pluck('timestamp')
//Get a new timestamp (remember this is *after* mouse out)
.timestamp()
//Compute the timeinterval
.map(x => x.timestamp - x.value)
//Add the new time interval to the running total
.scan((total, diff) => total += diff, 0)
.subscribe(x => console.log(x));
這個答案非常有幫助謝謝! – Rob