javascript - window.event fails when called from setTimeout -
i have page perform actions based on value of cookie. when page loads, want halt these actions holding shift key.
function checkforshift() { if (window.event.shiftkey) { alert('shift key detected, aborting'); return false } else { //do stuff } } this works great, unless call checkforshift() settimeout() function. so:
settimeout ( "checkforshift()" , 500 ); in case, checkforshift() function called after 500ms, next line fails message:
unable property 'shiftkey' of undefined or null reference why undefined when called settimeout() ?
the event valid when occurs (and don't use window.event provided event handling callback).
to achieve want, must store state detecting changes :
var shiftkeydown = false; window.onkeydown = function(e){ shiftkeydown = !!(e||window.event).shiftkey; } window.onkeyup = function(){ shiftkeydown = false; } function checkforshift() { if (shiftkeydown) { alert('shift key detected, aborting'); } else { //do stuff } } and when call settimeout, don't pass string function :
settimeout (checkforshift , 500 );
Comments
Post a Comment