From adcd44ac51f9d8ab9efcdcf787adf3bbd650a4e9 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 21 Jan 2022 13:27:27 +0100 Subject: [PATCH] wip #5009 @0.25 --- js/libs/cube/util.js | 69 +++++++++++++++++++ .../slideshow/fluidbook.slideshow.dummy.js | 47 +++++-------- 2 files changed, 87 insertions(+), 29 deletions(-) diff --git a/js/libs/cube/util.js b/js/libs/cube/util.js index 2712513e..a4b01d2d 100644 --- a/js/libs/cube/util.js +++ b/js/libs/cube/util.js @@ -165,6 +165,75 @@ Object.size = function (obj) { return size; }; +/** + * Behaves the same as setInterval except uses requestAnimationFrame() where possible for better performance + * @param {function} fn The callback function + * @param {int} delay The delay in milliseconds + */ +window.requestInterval = function(fn, delay) { + var start = new Date().getTime(), + handle = new Object(); + + function loop() { + var current = new Date().getTime(), + delta = current - start; + + if(delta >= delay) { + fn.call(); + start = new Date().getTime(); + } + + handle.value = requestAnimationFrame(loop); + }; + + handle.value = requestAnimationFrame(loop); + return handle; +} + +/** + * Behaves the same as clearInterval except uses cancelRequestAnimationFrame() where possible for better performance + * @param {int|object} fn The callback function + */ +window.clearRequestInterval = function(handle) { + window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) : + window.webkitCancelAnimationFrame ? window.webkitCancelAnimationFrame(handle.value) : + window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) : /* Support for legacy API */ + window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) : + window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) : + window.msCancelRequestAnimationFrame ? window.msCancelRequestAnimationFrame(handle.value) : + clearInterval(handle); +}; + +window.requestTimeout = function(fn, delay) { + + var start = new Date().getTime(), + handle = new Object(); + + function loop(){ + var current = new Date().getTime(), + delta = current - start; + + delta >= delay ? fn.call() : handle.value = requestAnimationFrame(loop); + }; + + handle.value = requestAnimationFrame(loop); + return handle; +}; + +/** + * Behaves the same as clearTimeout except uses cancelRequestAnimationFrame() where possible for better performance + * @param {int|object} fn The callback function + */ +window.clearRequestTimeout = function(handle) { + window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) : + window.webkitCancelAnimationFrame ? window.webkitCancelAnimationFrame(handle.value) : + window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) : /* Support for legacy API */ + window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) : + window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) : + window.msCancelRequestAnimationFrame ? window.msCancelRequestAnimationFrame(handle.value) : + clearTimeout(handle); +}; + /*! sprintf-js v1.1.2 | Copyright (c) 2007-present, Alexandru Mărășteanu | BSD-3-Clause */ !function () { "use strict"; diff --git a/js/libs/fluidbook/slideshow/fluidbook.slideshow.dummy.js b/js/libs/fluidbook/slideshow/fluidbook.slideshow.dummy.js index bc0b0d61..920b073d 100644 --- a/js/libs/fluidbook/slideshow/fluidbook.slideshow.dummy.js +++ b/js/libs/fluidbook/slideshow/fluidbook.slideshow.dummy.js @@ -1,14 +1,10 @@ function FluidbookDummySlideshow(fluidbook) { this.fluidbook = fluidbook; - this.intervals = []; + } FluidbookDummySlideshow.prototype = { clear: function () { - // console.log('clear'); - // $.each(this.intervals, function (k, v) { - // clearInterval(v); - // }); }, initSlideshow: function (s) { @@ -22,33 +18,26 @@ FluidbookDummySlideshow.prototype = { // Move first to end holder.append(holder.find('.fb-slideshow-slide:eq(0)')); - - this.intervals.push( - setInterval( - function () { - if(!$(holder).is(':visible')){ - return; - } - var current = holder.find('.fb-slideshow-slide.show').eq(0); - var next = $(current).nextAll('.fb-slideshow-slide:not(.show):eq(0)'); - if ($(next).length === 0) { - next = holder.find('.fb-slideshow-slide:eq(0)'); - } - - $(next).css({display: 'block', zIndex: 2}); - - setTimeout(function () { - $(next).addClass('show').one($this.fluidbook.support.getTransitionEndEvent(), function () { - $(current).removeClass('show').css('display', 'none'); - $(this).css({zIndex: ''}); - }); - }, 200); - + requestInterval( + function () { + if (!$(holder).is(':visible')) { + return; + } + var current = holder.find('.fb-slideshow-slide.show').eq(0); + var next = $(current).nextAll('.fb-slideshow-slide:not(.show):eq(0)'); + if ($(next).length === 0) { + next = holder.find('.fb-slideshow-slide:eq(0)'); } - , parseFloat(this.fluidbook.settings.inlineSlideshowDuration) * 1000) - ); + $(next).css({display: 'block', zIndex: 2}); + requestTimeout(function () { + $(next).addClass('show').one($this.fluidbook.support.getTransitionEndEvent(), function () { + $(holder).find('.fb-slideshow-slide.show').not(next).removeClass('show').css('display', 'none'); + $(this).css({zIndex: '', display: ''}); + }); + }, 200); + }, parseFloat(this.fluidbook.settings.inlineSlideshowDuration) * 1000); }, resizeInline() { -- 2.39.5