]> _ Git - fluidbook-html5.git/commitdiff
wip #5009 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 21 Jan 2022 12:27:27 +0000 (13:27 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 21 Jan 2022 12:27:27 +0000 (13:27 +0100)
js/libs/cube/util.js
js/libs/fluidbook/slideshow/fluidbook.slideshow.dummy.js

index 2712513ee578357c33f685933f647c1f5f8aea92..a4b01d2d84f7c77c0a6c24e1533e25a66e133113 100644 (file)
@@ -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 <hello@alexei.ro> | BSD-3-Clause */
 !function () {
     "use strict";
index bc0b0d610f4f1aecf4cb47c3423fa54c45da98e9..920b073d2b8d318d5ad8dbd28313c332ce3722c2 100644 (file)
@@ -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() {