Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I wait for ajax to complete to redirect a page?

I know how to wait for ajax to complete, but if I'm going to redirect a page after some ajax calls are fired off, should I wait for them to complete before the redirect? Does it matter?

like image 312
elijah Avatar asked Jul 18 '13 23:07

elijah


2 Answers

If you're confident that the ajax call will be successful, then once the ajax call has occurred, redirecting won't affect it as far as your server is concerned. But don't forget, the client could lose their connection or a number of errors could occur, so you should probably wait to make sure the calls were successful. Another thing to consider is whether or not your ajax calls will affect whatever page you're going to redirect to. In that case DEFINITELY wait before redirecting. You won't want your user to get redirected to a broken page because their connection was slow or your server had a hiccup.

like image 77
matty-d Avatar answered Sep 24 '22 09:09

matty-d


I recommend waiting and handling possible errors that might occur.

You gain safe connection, error spotting, better software, by doing the whole process synchronously, making the redirection coming in the end.

You might spend 0.1 second more of execution but who cares?

like image 21
Jonast92 Avatar answered Sep 22 '22 09:09

Jonast92