Reputation: 652
I have a button setup to click on inputs on a page , but when i click it , the site hangs for awhile. I'm wondering if there is a way to have the function click one at a time before moving to the next one , that may possibly not bog down the page when clicked ? Or a better method ?
function clearInput() {
$('input').click();
}
Upvotes: 1
Views: 33
Reputation: 11342
For Debug the freeze issue, do you have event bind to each input? might be jQuery click all of them at the same time triggered too many events, try to put a breakpoint or console.log on input events, see how many are triggered etc.
To unbind all event bind to input try this one, it also set all input to null:
setTimeout(function() {
_this.off();
_this.click();
_this.val(null);
}, interval)
For your question, this is a solution to click each input with delay.
Use jquery each()
on input, for each iteration creat a setTimeout with increase delay, I used 500ms for example, you can change it to 10ms or whatever.
function clearInput() {
var interval = 500;
$('input').each(function() {
var _this = $(this);
setTimeout(function() {
_this.click(); //this is the function you need to call
console.log(_this.val()); //for example only, remove later
_this.val(null); //for example only, remove later
}, interval)
interval += 500;
});
}
clearInput();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="1" />
<input value="2" />
<input value="3" />
<input value="4" />
<input value="5" />
Upvotes: 2