简体   繁体   中英

setinterval lose focus on input field

As I try to use setInterval function input fields in bootstrap modal get focus out

setInterval(function(){
    console.log("hi");
}, 5000);

input focus automatically out while try to write in an input field.

ISSUE

This answer might not be what you're looking for, and I'm not sure a real answer can be given but putting code in comments is ugly. Read comments for info about the question itself.

  • Happens in Google Chrome Android(unsure if version or phone or keyboard matters)
  • Happens with delayed functions like timeout and setinterval
  • I guessed it had to do with a repaint, but if a console.log triggers it as well... I have no idea

Below the code that resembles my production case and with the "fix" (for my case) in it.

setTimeout(function() {
    // Any var declarations don't seem to matter
    var foo = null;

    // Easy way out ... if needed you can delay to do stuff onBlur
    // Find current focused element and escape if its an input
    if (document.activeElement.nodeName === 'INPUT') {
        return;
    }   

    // Here some random style change that triggered the bug
    randomFunc();
}, 210);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM