简体   繁体   中英

HTML Click event inside setInterval

I want to call click() for a given id inside setInterval

setInterval(function(){
document.getElementById('test').click();
}, 10);

This click() is working for a tag by clicking from another tag. But not working when I am just calling it in setInterval or even calling the same line in script. Any way I can achieve this ?

Jquery click is also not working neither any trigger call.

setInterval(function(){
$('#test').trigger('click');
}, 10);

Here is the complete code for testing:-

 <html> <body> <p>Select a File to Load:</p> <input id="inputFileToLoad" type="file" onchange="loadImageFileAsURL();" /> <a id="testy" href="" onclick="document.getElementById('inputFileToLoad').click(); return false">Upload</a> <br /> <script type='text/javascript'> function loadImageFileAsURL() { alert("Its a Test"); } setTimeout(function(){ document.getElementById('testy').click(); }, 1000); </script> </body> </html> 

EDIT (check console)

 setInterval(function(){ $('#test').trigger('click'); }, 10); $(document).on('click', '#test', function() { console.log('clicked'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test">test</div> 

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