jQuery phonegap应用程序中Javascript的定时长按
这里有一个在Javascript中长按的好例子: 但它没有规定知道媒体的持续时间 如果我想根据媒体的长度做不同的事情,我不能使用那篇文章中的模式 我试图通过在('mousedown')on变量中保存当前时间来做类似的事情 然后在('mouseup')上计算时间差jQuery phonegap应用程序中Javascript的定时长按,javascript,jquery,cordova,mouseevent,jquery-events,Javascript,Jquery,Cordova,Mouseevent,Jquery Events,这里有一个在Javascript中长按的好例子: 但它没有规定知道媒体的持续时间 如果我想根据媒体的长度做不同的事情,我不能使用那篇文章中的模式 我试图通过在('mousedown')on变量中保存当前时间来做类似的事情 然后在('mouseup')上计算时间差。 这在“普通”浏览器中的普通Javasript页面中运行良好 但是在我的phonegap应用程序中发生了一些事情, 如果手指在屏幕上停留很长时间(例如5秒),则看起来没有调用mouseup事件 这是某种本机移动浏览器行为吗?我能以某种方
。
这在“普通”浏览器中的普通Javasript页面中运行良好
但是在我的phonegap应用程序中发生了一些事情,
如果手指在屏幕上停留很长时间(例如5秒),则看起来没有调用mouseup
事件
这是某种本机移动浏览器行为吗?我能以某种方式覆盖它吗
我使用的是普通jQuery,而不是jQuery mobile
有什么想法吗?您可以看看点击保持
和vmouseup
(handleTouchEnd()
第752行)事件是如何在源代码中实现的
由于它已经过测试和实现,我建议使用jquery mobile而不是jquery和modify(因为它已经处理了与每个移动浏览器相关的所有“怪癖”),并根据需要更改代码。
您可以检查确定单击或长按[jQuery]的时间。
function AddButtonEventListener() {
try {
var mousedowntime;
var presstime;
$("button[id$='" + buttonID + "']").mousedown(function() {
var d = new Date();
mousedowntime = d.getTime();
});
$("button[id$='" + buttonID + "']").mouseup(function() {
var d = new Date();
presstime = d.getTime() - mousedowntime;
if (presstime > 999/*You can decide the time*/) {
//Do_Action_Long_Press_Event();
}
else {
//Do_Action_Click_Event();
}
});
}
catch (err) {
alert(err.message);
}
}
请注意,如果出于某种原因不使用jQuery Mobile,此解决方案是有用的。
我使用了这篇文章,只是添加了一段代码
$.event.special.tap = {
distanceThreshold: 10,
timeThreshold: 350,
setup: function () {
var self = this,
$self = $(self);
// Bind touch start
$self.on('touchstart', function (startEvent) {
// Save the target element of the start event
var target = startEvent.target,
touchStart = startEvent.originalEvent.touches[0],
startX = touchStart.pageX,
startY = touchStart.pageY,
threshold = $.event.special.tap.distanceThreshold,
timeout,
expired = false;
function timerFired() {
expired = true;
}
function removeTapHandler() {
clearTimeout(timeout);
$self.off('touchmove', moveHandler).off('touchend', tapHandler).off('touchcancel', removeTapHandler);
};
function tapHandler(endEvent) {
removeTapHandler();
if (target == endEvent.target) {
if (expired) {
$.event.simulate('longtap', self, endEvent);
} else {
$.event.simulate('tap', self, endEvent);
}
}
};
// Remove tap and move handlers if the touch moves too far
function moveHandler(moveEvent) {
var touchMove = moveEvent.originalEvent.touches[0],
moveX = touchMove.pageX,
moveY = touchMove.pageY;
if (Math.abs(moveX - startX) > threshold || Math.abs(moveY - startY) > threshold) {
removeTapHandler();
}
};
// Remove the tap and move handlers if the timeout expires
timeout = setTimeout(timerFired, $.event.special.tap.timeThreshold);
// When a touch starts, bind a touch end and touch move handler
$self.on('touchmove', moveHandler).on('touchend', tapHandler).on('touchcancel', removeTapHandler);
});
}
})
所以,现在我有一个水龙头和一个龙头