I’m trying to click every item with a 200 millisecond interval, I wrote the following script however there seems to be an issue with the For Loop. Some one please tell me what you think is wrong with it.
function clickLink(elm) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
elm.dispatchEvent(evt);
}
function sel() {
elms = document.getElementsByClassName('uItem');
var inputs= elms;
var howbig= elms.length;
console.log(howbig);
for (var i=250;i<elms.length;i++)
{
setTimeout(clickLink(inputs[i]),200)
};
There’s 1400 uItem ‘s on the page.
The way your doing this won’t get you one click per 200ms, you will register all your clicks almost at the same time(the time a loop last to iterate 1000 items,almost instantly) so you basically would get your 1000+ aprox. click events fired almost simultaneously, I think that could cause issues, so to make sure they are executed once in a 200ms approx. do this:
UPDATE:
Updated the anonymouse function to passito avoid closure issue noted in comments.UPDATE 2: Added
window.iinside anonymous function to workaroundiscope issueUPDATE 3: Updated my code above to fix the interval at which each click event is fired, we were using
200 * ibut that is only correct ifistarts at zero, here it starts at 250 so the correct form is200 * (i-249)In spite of some comments, the
iscope issue it’s resolved usingwindow.i, see test here https://tinker.io/98fd4/1