input - Javascript, anyway to do a 'whileFocused' eventListener? (No JQuery) -


is there way implement or simulate 'while focused' bind on dom element? log message every second, starting when onfocus occurs, stops upon leaving.

considering this:

myinput.addeventlistener("focus", function(){     setinterval(function(){         console.log('mouse on:' + this)     }, 1000); }); 

the thing can think of using global variable like:

var isfocused = false; myinput.addeventlistener("focus", function(){     isfocused = true;     setinterval(function(){         if (isfocused)             console.log('mouse on:' + this)     }, 1000); }); 

then adding listener onblur toggle 'isfocused'

but feels.....just wrong. , plus setinterval continue firing off in background right?

slight improvement @scott schwalbe

(function(){     var intervalid;     myinput.addeventlistener("focus", function(){         intervalid = setinterval(function(){             console.log('mouse on:' + this)         }, 1000);     });     myinput.addeventlistener("blur", function(){         clearinterval(intervalid);      }); })(); 

1) wrap in invoked function expression keep global variable space pure.

2) 'unfocus' event 'blur'


Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -