How can you determine if it's not functioning properly?
Typically, there is very little time between the beforeunload event, the unload event, and the actual exit of the page. When a page unloads, all running scripts are terminated (the browser then closes the window or navigates to a user-provided address, for example).
The issue here may be that the browser doesn't have enough time to send an ajax request before the page is unloaded.
I have come across a few methods to guarantee that your final request before page unload is executed. One approach involves sending a request and then implementing a loop that runs for a specific number of milliseconds, delaying the unload event and ensuring that the ajax request can finish.
window.onbeforeunload = function() {
fetch("http://"+window.location.hostname+"/process_sc.php?cC=" + 1);
// in this section, we make the browser wait for 300 seconds before proceeding with the unload
var t = Date.now() + 300;
while(Date.now() < t) {};
}