Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t在事件侦听器被“删除”后添加它;removeEventListener“;只在手机上_Javascript_Jquery_Mobile_Event Listener - Fatal编程技术网

Javascript Can';t在事件侦听器被“删除”后添加它;removeEventListener“;只在手机上

Javascript Can';t在事件侦听器被“删除”后添加它;removeEventListener“;只在手机上,javascript,jquery,mobile,event-listener,Javascript,Jquery,Mobile,Event Listener,我是js和jquery的新手 我有密码: function dragEnd(){ OnDrag = false; wrapperHalfWidth = box.parent().width() * settings.animPartofScrennToSlide if (Math.abs(dragLengthX) > wrapperHalfWidth ){ this.removeEventListener((useMobileDrag ? "tou

我是js和jquery的新手

我有密码:

function dragEnd(){
    OnDrag = false;
    wrapperHalfWidth = box.parent().width() * settings.animPartofScrennToSlide 
    if (Math.abs(dragLengthX) > wrapperHalfWidth ){
        this.removeEventListener((useMobileDrag ? "touchstart" : "mousedown"), dragStart, false);
        this.removeEventListener((useMobileDrag ? "touchmove" : "mousemove"), dragMove, false);
        this.removeEventListener((useMobileDrag ? "touchend" : "mouseup"), dragEnd, false);
        this.removeEventListener("touchcancel", dragCancel, false);

        var Direction = dragLengthX > 0;
        settings.prevNextClickCallback(outerSlCounter, Direction ? FORWARD : BACK);

        setTimeout(function(){
            this.addEventListener((useMobileDrag ? "touchstart" : "mousedown"), dragStart, false);
            this.addEventListener((useMobileDrag ? "touchmove" : "mousemove"), dragMove, false);
            this.addEventListener((useMobileDrag ? "touchend" : "mouseup"), dragEnd, false);
            this.addEventListener("touchcancel", dragCancel, false);
        }, 500);
        return SlideTo(outerSlCounter + (Direction ? -1 : 1));
    }
    else{   
        dragLengthX = 0;
        box.css({
            '-webkit-transition-timing-function': settings.easingCss,
            '-webkit-transition-duration': settings.animDragTime + 'ms',
            '-webkit-transform': 'translate3d(' + dragLengthX + 'px, 0px, 0px)',
            'transition-timing-function': settings.easingCss,
            'transition-duration': settings.animDragTime + 'ms',
            'transform': 'translate3d(' + dragLengthX + 'px, 0px, 0px)'
        });
    }
    isDragging = false;
    originalX = 0;
};


this.addEventListener((useMobileDrag ? "touchstart" : "mousedown"), dragStart, false);
this.addEventListener((useMobileDrag ? "touchmove" : "mousemove"), dragMove, false);
this.addEventListener((useMobileDrag ? "touchend" : "mouseup"), dragEnd, false);
this.addEventListener("touchcancel", dragCancel, false);
这个问题在if(Math.abs(dragLengthX)>wrapperHalfWidth){…}节中。我需要删除500ms的事件处理程序,以防止其他函数(dragStart(event)和dragMove(event))启动


最重要的是它工作得很好。当Slidind函数工作时,它会删除一次事件。但在移动设备上,警报事件监听器不起作用

不要使用jQuery使用
addEventListener
removeEventListener
。改用
bind()
/
unbind()

if (Math.abs(dragLengthX) > wrapperHalfWidth ) {
  $(this)
  .bind("touchstart mousedown", dragStart)
  .bind("touchmove mousemove", dragMove)
  .bind("touchend mouseup", dragEnd)
  .unbind("touchcancel");

  var Direction = dragLengthX > 0;
  settings.prevNextClickCallback(outerSlCounter, Direction ? FORWARD : BACK);

  setTimeout(function () {
    $(this)
    .unbind("touchstart mousedown touchmove mousemove touchend mouseup")
    .bind("touchcancel", dragCancel);
  }, 500);

  return SlideTo(outerSlCounter + (Direction ? -1 : 1));
}

尽管我会发现如果您使用一个标志来告诉事件处理程序是否应该做任何事情,而不是不断地绑定和取消绑定它们,这会更加优雅。

代码中没有任何内容建议您使用jquery——另一个functions@SidorukS:你甚至在应用程序中使用jQuery吗?另外,“不工作”是一个可怜的错误描述,请更精确一些。我重写了TwilightShow()插件的一些部分,并添加了拖放功能-cod的这一部分在桌面浏览器(Chrome)上非常有效,但当我在ipodtouch上试用它时,它在第一次滑动后就停止工作了。