Over the last days, my team have been working on a front-end project.
We’ve implemented two ajax requests, inside a collapsing box content, that needed to fade out a single spinner loader when both requests had finished.
But, wait a minute. Thinking on the DRY concept, maybe someone has already done this job for us.
After googling a little, we’ve found that jQuery already done it. I’m talking about the jQuery.when method.
This was exactly what we were looking for. Its usage is really simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
As you could see, you only need to pass the ajax requests as parameters to
jQuery.when (even if one of them is not a jQuery ajax request), and the success and failure callback functions to
Doing only this, jQuery will handle your requests for you and will execute the callbacks after all requests have been finished.
So, we’ve decided to use this jQuery implementation to handle the ajax requests. So far, it is meeting our expectations.