var canvas = document.getElementById('myCanvas'); var timer = null; canvas.onmousedown = function(){ timer = setTimeout( doStuff, 2000 ); }; canvas.onmouseup = function(){ clearTimeout( timer ); }; function doStuff() { alert('hello, you just pressed the mousebutton for two seconds.') }