简体   繁体   中英

Issue with recursive click on same element

Recurivesly called onclick on same element only twice but click action doesn't follow default action, Idea is that when user click on <input /> it should not popup the file explorer but at first do some async task and then call click action without user gesture, which in turn open file explorer.

Sample problem

https://jsbin.com/zuzinen/edit?html,js,console,output

<body>
  <input id="inp" type="file" />
</body>


var clicked = false;

function takePermission(cb) {
  setTimeout(function() {
      clicked = true;
      cb();
    }, 2000);
}

function handleClick(e) {
  console.log(clicked)
  if (!clicked) {
    e.preventDefault();
    takePermission(function() {
      document.getElementById('inp').click();
    });

  }
}

document.getElementById('inp').onclick = handleClick;

After the first click you assign clicked to true, this means that your if statement does not fire.

So there is no more calls to click() after the first one.

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