When is it appropriate to use synchronous ajax?

后端 未结 4 1449
后悔当初
后悔当初 2020-12-10 02:32

I was just reading another question about jQuery\'s synchronous ajax call, and I got to wondering:

What circumstances make a synchronous version of an

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 02:53

    synchronous ajax is often used to retrieve a valued from the server which is required to further continue processing of client side code. in such case, the ajax call will block until the call returns with the desired value. example:

    a javascript function needs to compute salary for an employee: step1 : get the employee id from the form step2 : make a sync server call passing the emp.id to get his salary/hour step3 : multiply salary rate by number of working hours

    as you can see, total salary cannot be computed unless the server call is finished so this should be a sync function, although if using jquery, one could handle onSuccess to compute the salary asynchronously but processing will continue in this if you have a message box to display the salary, it will appear empty...

提交回复
热议问题