2016-03-30 57 views
0

如何讓這個js函數在觸摸屏幕一秒鐘後執行 和 如何使它成爲同一觸摸位置的一秒鐘..如果我繼續觸摸屏幕在相同的位置一秒鐘?如何在函數執行前等待一秒

function onCanvasTouchStart(event) 
{ 
    if(event.touches.length == 1) 
    { 
     // draw 
     event.preventDefault(); 
     brush.strokeStart((event.touches[0].pageX) - canvas.offsetLeft, (event.touches[0].pageY) - canvas.offsetTop); 
     window.addEventListener('touchmove', onCanvasTouchMove, false); 
     window.addEventListener('touchend', onCanvasTouchEnd, false); 
    } 
} 

回答

1

使用setTimeOut

function onCanvasTouchStart(event){ 
    if(event.touches.length == 1){ 
     setTimeout(function(){ 
      // draw 
      event.preventDefault(); 

      brush.strokeStart((event.touches[0].pageX) - canvas.offsetLeft, (event.touches[0].pageY) - canvas.offsetTop); 

      window.addEventListener('touchmove', onCanvasTouchMove, false); 
      window.addEventListener('touchend', onCanvasTouchEnd, false); 
     }, 1000); 

    } 

} 
+0

我該如何使它成爲同一觸摸位置的一秒?如果我在同一位置持續觸摸屏幕一秒鐘? –

+0

像@ManoDestra說的,你需要跟蹤觸摸開始/結束時間。我粗略地提出了一些代碼來演示這個小提琴的想法。 https://jsfiddle.net/keL9pbar/。 – arifin4web

1
function onCanvasTouchStart(event) { 
    if (event.touches.length == 1) { 
     setTimeout(function() { 
      // draw 
      event.preventDefault(); 

      brush.strokeStart((event.touches[0].pageX) - canvas.offsetLeft, (event.touches[0].pageY) - canvas.offsetTop); 

      window.addEventListener('touchmove', onCanvasTouchMove, false); 
      window.addEventListener('touchend', onCanvasTouchEnd, false); 
     }, 1000); 
    } 
} 

任何觸摸屏幕,一秒鐘後這之後會發生,但如果你中持續1秒觸摸屏幕上後需要它,那麼你就需要確保觸摸WASN」結束於此內部函數被調用之前的任何時刻。您可以存儲觸摸開始/結束的日期。如果觸摸開始時發生的任何觸摸事件發出setTimeout信號,您將不會繼續使用內部函數的代碼。

+0

我怎樣才能使它成爲同一觸摸位置的一秒?如果我繼續在同一位置觸摸屏幕一秒鐘? –

+0

在這種情況下,您必須確保觸摸移動事件不會讓您距離增量平均值太遠。所以他們的手指仍然保持得很充分。或者,如果您想嚴格要求,請確保自開始以來沒有發生移動事件。 – ManoDestra

相關問題