1

I have the following functionality and am unsuccessfully trying to add a pause after each request of 2 seconds as to not exceed the RPM threshold.

<cfoutput>
<script type="text/javascript" language="JavaScript">

    // Convert ColdFusion variable to JS variable
    #ToScript(Variables.resourceIds, "jsList")#

    // Split list
    var list = jsList.split(",");

    // Loop through list
    for (var i=0; i<list.length; i++) {         
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[i], false);
        pricingSearch.onload = function (e) { };
        pricingSearch.onerror = function (e) { };
        pricingSearch.send(null);
        console.log('Searching for Id ' + list[i] + '...');
        setTimeout(function() {
            // Wait for a couple seconds
        }, 2000);
        if (i == list.length) {
            console.log('All done!');
        }           
    }
</script>
</cfoutput>

This just doesn't appear to do anything as the loop completes as quickly as it can.

I'd even tried adapting the code to this:

for (var i=0; i<list.length; i++) {         
    setTimeout(function(list,i) {               
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[i], false);
        pricingSearch.onload = function (e) { };
        pricingSearch.onerror = function (e) { };
        pricingSearch.send(null);
        console.log('Searching for Resource Id ' + list[i] + '...');
        if (i == list.length) {
            console.log('All done!');
        }
    }, 2000);       
}

But this also does not work :(

Does anyone else know of any other methods I can use to achieve this?

Thanks

CPB07
  • 679
  • 3
  • 13
  • 23
  • See [How do I add a delay in a JavaScript loop?](http://stackoverflow.com/questions/3583724/how-do-i-add-a-delay-in-a-javascript-loop) – apsillers Aug 12 '14 at 14:28
  • Your second snippet will set a `list.length` number of timeout functions which will all execute at once after the 2000ms delay (i.e. at the same time) – blgt Aug 12 '14 at 14:39

1 Answers1

2

You can use delay parameter, so you can delay each request 2 sec. later. But if there is limits on request, you can try to call next request on complete of the previous request.

var delay = 2000;
for (var i=0; i<list.length; i++) {         
    setTimeout(function(list,i) {               
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[i], false);
        pricingSearch.onload = function (e) { };
        pricingSearch.onerror = function (e) { };
        pricingSearch.send(null);
        console.log('Searching for Resource Id ' + list[i] + '...');
        if (i == list.length) {
            console.log('All done!');
        }
    }, delay);
    delay += 2000;
}


//alternative
function callRequest(index){
        if (index == list.length) {
            console.log('All done!');
            return;
        }
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[index], false);
        pricingSearch.onload = function (e) { callRequest(index+1); };
        pricingSearch.onerror = function (e) { console.log('error on index:' + index);callRequest(index+1)};
        pricingSearch.send(null);
        console.log('Searching for Resource Id ' + list[index] + '...');


}
callRequest(0);
ymutlu
  • 6,585
  • 4
  • 35
  • 47
  • Waiting on the previous request to complete is ideal in this situation. There is still a chance the RPM threshold could be exceeded with this method but I escape that by timing the request in the doPricing page and sleeping in there. – CPB07 Aug 12 '14 at 15:09