Kevin Boss
Kevin Boss

Reputation: 145

Event when call ends / JavaScript

We want to log the JavaScript on our Website. For that I am overwriting the log functionality with my own implementation.

On the server side we are using Log4Net for our logging. My idea now is to capture the log on the client side and send it to the server with ajax. How ever I don't want to send the Ajax request every time someone types log. So my idea was to catch all the logs and send ONE request after the call-stack ended up where it has began.

Is there an event or a way to detect when call-stack has "ended" ?

Upvotes: 1

Views: 323

Answers (1)

Habibillah
Habibillah

Reputation: 28695

You can try to use javascript beforeunload event to send your log when user leave a page. If you use jQuery, the code look like:

$(window).bind("beforeunload",function(event) {
    //... put your ajax code here ...

    return "The log has sent to server.";
});

however beforeunload event will show a message box on your screen.

Also if you want to run a function after another function run, you can use jQuery $.when() method as below:

$.when(functionOne(), functionTwo()).then(functionThree()).done(functionFour());

functionOne(), functionTwo(), functionThree(), and functionFour() must be a deferred object. This blog give a simple explanation how to implement deferred object.

Upvotes: 1

Related Questions