我正在使用here
找到的代码版本var startTime, endTime;
document.getElementById(queryid).addEventListener('touchstart',function() {
startTime = new Date().getTime();
},false);
document.getElementById(queryid).addEventListener('touchend',function(event) {
endTime = new Date().getTime();
if((endTime-startTime) > 1000)
alert('tap hold event');
},false);
它会在某些动作之前点击并保持“技巧”,但我的问题是只有当我停止触摸屏幕时才触发动作,而我想要在敲击至少1秒后触发动作,无论我是否不要再触摸屏幕..
这个似乎更接近我想要的东西,但似乎并不完整......(据我所知...... gbStillTouching总是假的?我没有看到任何地方被切换.. gnstarttime == nid?是不是nid = gnstarttime?
var gnStartTime = 0;
var gbMove = false;
var gbStillTouching = false;
function checkTapHold(nID) {
if ((!gbMove) && (gbStillTouching) && (gnStartTime == nID)) {
gnStartTime = 0;
gbMove = false;
alert('tap hold event');
}
}
window.addEventListener('touchstart',function(event) {
gnStartTime = Number(new Date());
setTimeout('checkTapHold(' + gnStartTime + ');clearTimeout();',2000);
},false);
window.addEventListener('touchmove',function(event) {
gbMove = true;
},false);
window.addEventListener('touchend',function(event) {
gbStillTouching = false;
},false);
答案 0 :(得分:2)
这是问题的解决方案,经过朋友的一些有用的帮助..
var gnStartTime = 0;
var gbStillTouching = false;
function checkTapHold(nID) {
if ((gbStillTouching) && (gnStartTime == nID)) {
gnStartTime = 0;
lastClickedId = queryid;
}
}
document.getElementById(queryid).addEventListener('touchstart',function(event) {
gnStartTime = Number(new Date());
gbStillTouching = true;
setTimeout(function() {checkTapHold(gnStartTime);clearTimeout();},1000);
},false);
document.getElementById(queryid).addEventListener('touchend',function(event) {
gbStillTouching = false;
},false);
这完美地工作..在敲击至少1秒后,我可以采取必要的行动.. :)