How can I execute a function if a user cancels a download in browser during the operation in JavaScript?

I’m using the following JavaScript function to initiate a file download:

function save(object, mime, name) {
  var a = document.createElement('a');
  var url = URL.createObjectURL(object);
  a.href = url;
  a.download = name;
  a.click();
}

This function creates a download link and simulates a click to start the download process. However, I want to run a specific function if the user cancels the download or closes the tab during this operation.

Cancel Button

Is there a way to detect if the download is canceled or interrupted so that I can execute a function or handle the situation appropriately? If not, is there a workaround to achieve this?

I understand that JavaScript in the browser has limited access to the download status after the user initiates it, but any suggestions or workarounds would be greatly appreciated!