.animate() not running from time to time.. why?!?
I am having an issue where an animate function is not running periodically.
so to start with i have a piece of code that listens for a swipe on an element
- var maxTime = 1000,
- // allow movement if < 1000 ms (1 sec)
- maxDistance = 50,
- // swipe movement of 50 pixels triggers the swipe
- target = jQuery('.pageSize'),
- startX = 0,
- startTime = 0,
- touch = "ontouchend" in document,
- startEvent = (touch) ? 'touchstart' : 'mousedown',
- moveEvent = (touch) ? 'touchmove' : 'mousemove',
- endEvent = (touch) ? 'touchend' : 'mouseup';
-
- target.bind(startEvent, function(e) {
- // prevent image drag (Firefox)
- // e.preventDefault();
- startTime = e.timeStamp;
- startX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX;
- }).bind(endEvent, function(e) {
- startTime = 0;
- startX = 0;
- }).bind(moveEvent, function(e) {
- // e.preventDefault();
- var currentX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX,
- currentDistance = (startX === 0) ? 0 : Math.abs(currentX - startX),
- // allow if movement < 1 sec
- currentTime = e.timeStamp;
- if (startTime !== 0 && currentTime - startTime < maxTime && currentDistance > maxDistance) {
- if (currentX < startX) {
- // swipe left code here
-
- console.log("page forward trigger");
- jQuery(window).trigger('swipeForward');
-
- }
- if (currentX > startX) {
- // swipe right code here
- console.log("page back trigger");
- //slide("back", pageSize);
- jQuery(window).trigger('swipeBackward');
- }
- startTime = 0;
- startX = 0;
- }
- });
if a user swipes left for example it triggers the event swipeForward
so this sets off the next piece of code
- jQuery(window).on('swipeForward', swipeHandlerNext );
- jQuery(window).on('swipeBackward', swipeHandlerPrev );
I am using swipe handlers so that a user cant swipe twice and create a double event
this would then say execute the swipeHandlerNext function
- function swipeHandlerNext(event) {
-
- // If event isn't already marked as handled, handle it
- if(event.handled !== true) {
-
- // Kill event handler, preventing any more clicks
- jQuery(".pageSize").off("swipeForward");
-
- // Do your stuff here
- pageSize = jQuery(".mainHeader").width();
- slide("forward", pageSize);
-
- console.log(" swipe complete page forward via swipe");
-
- // Mark event as handled
- event.handled = true;
- }
-
- return false;
- }
this obviously executes the slide function. this is the one that has .animate command
- function slide(data, pageSize) {
-
-
- if (!pageSize) {
- pageSize = jQuery(".mainHeader").width();
- }
-
- var promise = calcLeft(data, pageSize);
-
-
- jQuery.when(promise).then(function(result) {
- console.log(result);
- console.log("reached here");
-
- jQuery('#pageHolder').addClass("test2" + result).delay(500).animate({
- left: result
-
- }, 400, function() {
- console.log("animation started");
- calcNav(pageSize);
- calcPage(pageSize);
- jQuery(".touchslider-prev").on("click", clickHandlerPrev);
- jQuery(".touchslider-next").on("click", clickHandlerNext)
- jQuery(".pageSize").on("swipeForward", swipeHandlerNext);
- jQuery(".pageSize").on("swipeBackward", swipeHandlerPrev);
- console.log("animation complete");
-
- });
-
- console.log (jQuery('#pageHolder'));
- });
-
- }
so as i was saying on iPad and iPadMini when I swipe sometimes it doesnt work. Its really sporadic sometimes i can swipe 10 times and it crashes and other times it fails on the first time.
I know its failing on the animate as the times it doesnt work i get no console log of animation start or finish
I also wonder if the prevent double slide is working as in the console if i swipe and it doesnt work i can do this multiple times and get the logs for "swipe complete page forward via swipe" - surely this should show as the jQuery(".pageSize").on("swipeForward", swipeHandlerNext); is only set within the animation complete.
I have other events that trigger slide like buttons and they never fail
Thanks for any help