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

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -