Question

Problem description:
I'm making a post request inside of $.each loop:

$.each(companies, function() {
    appAPI.request.post({
        url: url,
        postData: postdata,
        onSuccess: onSuccess(),
        onFailure: onFailure()
    });
})

The syntax (I'm using crossrider.com API) is similar to jQuery, but still different - 'post' method doesn't have 'timeout' option. How can I check if it takes more than 3 seconds to receive a response, and if it is - abandon this request (don't call onSuccess() function)?

jQuery here can be used too, but I have to do appAPI.request.post instead of $.post for some reasons.

Was it helpful?

Solution

You can use the boolean variable. e.g. isThreeSecElapsed and set it true in OnSuccess & Failure.
You will also have to use the setInterval from javascript which will keep on reading isThreeSecElapsed until 3 seconds. If it is not set to true after 3 seconds. You can do your required code there.
You can wirte return directly at start of your Success method so that operations inside it will not be executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top