Jquery Browser's stop button event

前端 未结 1 992
情歌与酒
情歌与酒 2021-01-21 08:00

Is there a way to detect when the user has clicked the stop button on any browser? I want to stop the script from running when the user decides to click the stop button when upl

1条回答
  •  野的像风
    2021-01-21 08:47

    Unfortunately the browser's stop button doesn't affect AJAX requests. If you want to cancel an AJAX request you can abort it like below:

    var xhr = $.ajax(
        url: "http://someurl.org",
        success: function() { ... }
    );
    
    xhr.abort();
    

    The button to trigger the abort will need to be in your HTML since the browser's stop button cannot abort the request. If the user went to another page the browser would stop listening though and this is essentially what happens when you call xhr.abort().

    0 讨论(0)
提交回复
热议问题