Javascript submit before moving to next step

100 views Asked by At

I'm a novice with javascript but can usually make due. In this case, I'm having a problem with the first portion of the code submitting before executing the next portion of the code. The idea is to trigger a click, wait 5 seconds, then trigger a click on the next element in the list. If I run these individually, one at a time, it works without issue. If I run these together it is as if the first click doesn't even happen. I'm working to simulate some clicks on a MicroStrategy Dossier. Once this is working, I'll iterate through all of the div items dynamically by counting the elements first.

Any help/advice is appreciated.

var t1 = new Date();
var t2 = new Date();
var dif = 0;

//Click first element in the list
var n = "div.item[idx=\"0\"]";
$('div.scroll-container.mstrmojo-scrollNode').find(n).each(function(){
        $(this).trigger('click');   
            });

//wait  5 seconds
t2 = new Date();
        dif = t1.getTime() - t2.getTime();
            while(dif < 5000){
                t2 = new Date();
                dif = t2.getTime() - t1.getTime();  
            };

//Click second element in the list
var n = "div.item[idx=\"1\"]";
$('div.scroll-container.mstrmojo-scrollNode').find(n).each(function(){
        $(this).trigger('click');   
            });
1

There are 1 answers

2
Andrez On

JavaScript will not wait for the 5 seconds in your code before going for the next part. You have to place the second click inside a setTimeout:

setTimeout(function(){ 
    //Click second element in the list
    var n = "div.item[idx=\"1\"]";
    $('div.scroll-container.mstrmojo-scrollNode').find(n).each(function(){
        $(this).trigger('click');
    });
}, 5000);