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

2
  • See How do I add a delay in a JavaScript loop? Commented Aug 12, 2014 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) Commented Aug 12, 2014 at 14:39

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.