I want to call api before user navigates to url using href or reloading the current page. Here's my code that i have used.
@HostListener('window:unload', ['$event'])
unloadHandler(event) {
console.log('window:unload');
this.storeSessionVistorsCount();
}
storeSessionVistorsCount() {
if (this.sessionTime > 0) {
const body = {
ip: this.ipAddress,
space_id: this.spaceId,
type: 'session',
time: this.sessionTime
};
this.statisticService.getIpDetail(body).subscribe((res: any) => {
console.log('success')
}, err => {
console.log(err);
});
}
}
Api is cancelled by the browser. Can anyone help me out . Thanks.