In order to implement a "long press" on mobile, I started with a simple event map where touchstart
sets a time, touchend
sets another and then calculates the difference to see how long the element was pressed. This was my old code:
$('html')
.on({
touchstart : function(e){
g.tt.start = new Date();
},
touchend : function(e){
g.tt.end = new Date();
g.tt.delta = g.tt.end - g.tt.start;
alert(g.tt.delta);
g.tt = {};
}
})
;
but unfortunately...
...every other press kept calculating the difference from the previous touchend to the touchstart. I'm pretty sure there is something basic I'm missing and thus end up over-engineering this (no formal training I'm afraid). Here is my new code:
$('html')
.on({
touchstart : function(e){
g.tt = {};
g.tt.start = new Date();
},
touchend : function(e){
g.tt.end = new Date();
g.tt.delta = g.tt.end - g.tt.start;
if( isNaN(g.tt.delta) == false ) {
alert(g.tt.delta);
}
else {
return false;
}
setTimeout(function(){g.tt = {}; });
}
})
;
Shouldn't there be a much easier way to do this with less clauses? Never mind my funny g.tt
variable names.