From 2234b962eef0ae21fca006362479428e80378a8d Mon Sep 17 00:00:00 2001 From: kou_hin Date: Thu, 25 May 2017 01:51:55 +0900 Subject: [PATCH] Fix bug for safari --- src/watchOnce.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/watchOnce.js b/src/watchOnce.js index 672460a..47fe773 100644 --- a/src/watchOnce.js +++ b/src/watchOnce.js @@ -11,11 +11,8 @@ if (canUseDOM) { require('intersection-observer'); } -const animationFrame = typeof window !== 'undefined' - ? (window.requestAnimationFrame || - window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - window.setTimeout) : setTimeout; +const ric = typeof window !== 'undefined' + ? (window.requestIdleCallback || window.setTimeout) : setTimeout; const observers = {}; const callbacks = new WeakMap(); @@ -24,18 +21,20 @@ function findObserver(offsetString) { let observer = observers[offsetString]; if (!observer) { observer = new IntersectionObserver((entries) => { + if (entries.length < 1) { + return; + } for (let i = 0, len = entries.length; i < len; i += 1) { const entry = entries[i]; - if (entry.intersectionRatio < 1) { - return; - } - const element = entry.target; - const callback = callbacks.get(element); - if (callback) { - animationFrame(callback); - callbacks.delete(element); + if (entry.isIntersecting || entry.intersectionRatio > 0) { + const element = entry.target; + const callback = callbacks.get(element); + if (callback) { + ric(callback); + callbacks.delete(element); + } + observer.unobserve(element); } - observer.unobserve(element); } }, { rootMargin: offsetString,