Javascript 在第一次刷卡后禁用刷卡,然后在完成后重置?

Javascript 在第一次刷卡后禁用刷卡,然后在完成后重置?,javascript,jquery,ios,Javascript,Jquery,Ios,我有下面的代码,它允许我滑动一个元素,元素将移动,露出下面的元素。我想能够刷一次,有功能运行,有div重置它的位置,并允许我再次刷。基本上,在功能运行时禁用刷卡,然后在功能结束后启用 这是我的密码: var threshold = { x: 30, y: 10 } var originalCoord = { x: 0, y: 0 } var finalCoord = { x: 0, y: 0 } function touchMove(ev

我有下面的代码,它允许我滑动一个元素,元素将移动,露出下面的元素。我想能够刷一次,有功能运行,有div重置它的位置,并允许我再次刷。基本上,在功能运行时禁用刷卡,然后在功能结束后启用

这是我的密码:

var threshold = {
    x: 30,
    y: 10
}
var originalCoord = {
    x: 0,
    y: 0
}
var finalCoord = {
    x: 0,
    y: 0
}

    function touchMove(event) {
        finalCoord.x = event.targetTouches[0].pageX
        changeX = originalCoord.x - finalCoord.x
        var changeY = originalCoord.y - finalCoord.y
        if (changeY < threshold.y && changeY > (threshold.y * -1)) {
            changeX = originalCoord.x - finalCoord.x
            if (changeX > threshold.x) {
                // My function which runs when you swipe left
            }
        }
    }

    function touchEnd(event) {
    }

    function touchStart(event) {
        originalCoord.x = event.targetTouches[0].pageX
        finalCoord.x = originalCoord.x
    }

window.addEventListener("touchstart", touchStart, false);
window.addEventListener("touchmove", touchMove, false);
window.addEventListener("touchend", touchEnd, false);
var阈值={
x:30,
y:10
}
var originalCoord={
x:0,,
y:0
}
var FinalCord={
x:0,,
y:0
}
功能触摸移动(事件){
finalcord.x=event.targetTouches[0].pageX
changeX=原始订单.x-最终订单.x
var changeY=原始订单y-最终订单y
if(changeY(threshold.y*-1)){
changeX=原始订单.x-最终订单.x
如果(changeX>threshold.x){
//向左滑动时运行的我的函数
}
}
}
函数touchEnd(事件){
}
功能触摸启动(事件){
OriginalCord.x=event.targetTouches[0]。pageX
finalcord.x=originalcord.x
}
window.addEventListener(“touchstart”,touchstart,false);
window.addEventListener(“touchmove”,touchmove,false);
window.addEventListener(“touchend”,touchend,false);

我想我可以使用
event.preventDefault()
return false
在函数运行时禁用拖动,但它仍然允许我在运行期间拖动。

很难弄清楚您想要什么,但要禁用滑动,只需添加辅助变量:

var _swipeDisabled = false;
然后在touchmove中检查是否禁用了刷卡,如果禁用,则返回false:

function touchMove(event) {
    if (_swipeDisabled) return false; // this line is crucial
    finalCoord.x = event.targetTouches[0].pageX
    changeX = originalCoord.x - finalCoord.x
    var changeY = originalCoord.y - finalCoord.y
    if (changeY < threshold.y && changeY > (threshold.y * -1)) {
        changeX = originalCoord.x - finalCoord.x
        if (changeX > threshold.x) {
            _swipeDisabled = true; // add this before calling your function
            // My function which runs when you swipe left
        }
    }
}

在这里调用的函数中。记住,最简单的解决方案通常是最好的

我可以通过删除然后在
EventListener
中重新添加来解决这个问题:

if (changeX > threshold.x) {
    window.removeEventListener('touchmove', touchMove, false);
    // Function
    setTimeout(function () {
        window.addEventListener('touchmove', touchMove, false);
    }, 800)
}

“我想我清楚地表明了我想做什么,什么是难以理解的?”查理关于重置div的部分-我想你也希望我们编写代码来重置div的位置。读完描述中代码前的最后一句话,一切都很清楚。
if (changeX > threshold.x) {
    window.removeEventListener('touchmove', touchMove, false);
    // Function
    setTimeout(function () {
        window.addEventListener('touchmove', touchMove, false);
    }, 800)
}