javascriptsettimeouttouch-eventjquery-eventstouchstart

Detect mousedown and touchstart pageX on another function


I have an event to detect mouse or touch and send this event to another function using setTimeout.

I have no problem detecting mouse or touch event, what I can not do is send touch event to the function.

$(divst + ' #sortable1').on('touchstart mousedown',function(e){
    e.preventDefault();
        setTimeout(function(){
                    StarTrans(e); // <- sending event
        },200);
});
function StarTrans(e){
    if(e.pageX==='undefined'){ // this does not work
        $(dtarget).offset({left:e.changedTouches[0].pageX-33,top:e.changedTouches[0].pageY-58});
        $('#titring').html(e.changedTouches[0].pageX + ',' + e.changedTouches[0].pageY);
    }else{ // this works
        $(dtarget).offset({left:e.pageX-33,top:e.pageY-58});
        $('#titring').html(e.pageX + ',' + e.pageY);
    }
}

mousedown works as expected, touchstart returns 'undefined'.

I tried these events to get pageX

Thank you!


Solution

  • Firstly, if(e.pageX==='undefined'){ it's not correct. Better if (typeof e.pageX == 'undefined') { if you need that condition, more better if (e.type == 'touchstart') {.