What does “async: false” do in jQuery.ajax()?

后端 未结 7 1890
北海茫月
北海茫月 2020-11-22 01:47

Specifically, how does it differ from the default ( async: true ) ?

In what circumstances would I want to explicit set async to fals

7条回答
  •  情书的邮戳
    2020-11-22 02:47

    One use case is to make an ajax call before the user closes the window or leaves the page. This would be like deleting some temporary records in the database before the user can navigate to another site or closes the browser.

     $(window).unload(
            function(){
                $.ajax({
                url: 'your url',
                global: false,
                type: 'POST',
                data: {},
                async: false, //blocks window close
                success: function() {}
            });
        });
    

提交回复
热议问题