Touch move getting stuck Ignored attempt to cancel a touchmove
Asked Answered
C

6

74

I'm messing around with touch events on a touch slider and I keep getting the following error:

Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted.

I'm not sure what is causing this problem, I am new to working with touch events and can't seem to fix this problem.

Here is the code handling the touch event:

Slider.prototype.isSwipe = function(threshold) {
    return Math.abs(deltaX) > Math.max(threshold, Math.abs(deltaY));
}


Slider.prototype.touchStart = function(e) {

    if (this._isSliding) return false;

      touchMoving = true;
      deltaX = deltaY = 0;

    if (e.originalEvent.touches.length === 1) {

        startX = e.originalEvent.touches[0].pageX;
        startY = e.originalEvent.touches[0].pageY;

        this._$slider.on('touchmove touchcancel', this.touchMove.bind(this)).one('touchend', this.touchEnd.bind(this));

        isFlick = true;

        window.setTimeout(function() {
            isFlick = false;
        }, flickTimeout);
    }
}


Slider.prototype.touchMove = function(e) {

    deltaX = startX - e.originalEvent.touches[0].pageX;
    deltaY = startY - e.originalEvent.touches[0].pageY;

    if(this.isSwipe(swipeThreshold)) {
        e.preventDefault();
        e.stopPropagation();
        swiping = true;
    }
    if(swiping) {
        this.slide(deltaX / this._sliderWidth, true)
    }
}


Slider.prototype.touchEnd = function(e) {

    var threshold = isFlick ? swipeThreshold : this._sliderWidth / 2;

    if (this.isSwipe(threshold)) {
        deltaX < 0 ? this.prev() : this.next();
    }
    else {
        this.slide(0, !deltaX);
    }

    swiping = false;

    this._$slider.off('touchmove', this.touchMove).one(transitionend, $.proxy(function() {
        this.slide(0, true);
        touchMoving = false;
    }, this));
}

You can find the actual slider here at this pen.

If you swipe through fast enough it will throw the error and sometimes get stuck in the middle of a swipe. Still can't wrap my head around why it is not working. Any help/insight would be greatly appreciated. Not sure what I am doing wrong.

Curd answered 21/10, 2014 at 3:9 Comment(0)
W
53

The event must be cancelable. Adding an if statement solves this issue.

if (e.cancelable) {
   e.preventDefault();
}

In your code you should put it here:

if (this.isSwipe(swipeThreshold) && e.cancelable) {
   e.preventDefault();
   e.stopPropagation();
   swiping = true;
}
Womanhater answered 15/11, 2018 at 8:41 Comment(0)
E
16

I know this is an old post but I had a lot of issues trying to solve this and I finally did so I wanted to share.

My issue was that I was adding an event listener within the ontouchstart and removing it in the ontouchend functions - something like this

function onTouchStart() {
  window.addEventListener("touchmove", handleTouchMove, {
    passive: false
  });
}

function onTouchEnd() {
  window.removeEventListener("touchmove", handleTouchMove, {
    passive: true
  });
}

function handleTouchMove(e) {
  e.preventDefault();
}

For some reason adding it removing it like this was causing this issue of the event randomly not being cancelable. So to solve this I kept the listener active and toggled a boolean on whether or not it should prevent the event - something like this:

let stopScrolling = false;

window.addEventListener("touchmove", handleTouchMove, {
  passive: false
});

function handleTouchMove(e) {
  if (!stopScrolling) {
    return;
  }
  e.preventDefault();
}

function onTouchStart() {
  stopScrolling = true;
}

function onTouchEnd() {
  stopScrolling = false;
}

I was actually using React so my solution involved setting state, but I've simplified it for a more generic solution. Hopefully this helps someone!

Equivoque answered 21/6, 2019 at 13:37 Comment(0)
B
12

I had this problem and all I had to do is return true from touchend and the warning went away.

Baerl answered 4/12, 2014 at 4:15 Comment(2)
Doesn't change anything hereKirghiz
I added e.cancelable to a if-statement before trying to call e.preventDefault() inside this statement.Application
R
8

Calling preventDefault on touchmove while you're actively scrolling is not working in Chrome. To prevent performance issues, you cannot interrupt a scroll.

Try to call preventDefault() from touchstart and everything should be ok.

Relax answered 15/10, 2015 at 15:51 Comment(2)
Then I just get: Ignored attempt to cancel a touchstart eventGeminate
Event listeners are invoked in the order of attachment. @Geminate perhaps you're adding touchstart event handler(s) before the event handler that calls preventDefault() and Chrome already began the logic (scroll, pinchzoom) that prevents canceling of touchstart/touchmove events.Dissatisfaction
N
2

Please remove e.preventDefault(), because event.cancelable of touchmove is false. So you can't call this method.

Noella answered 28/6, 2018 at 6:25 Comment(0)
S
0

If it is an image, you can just set 'touch-action' to none in css.

Silky answered 15/8, 2022 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.