I've been struggling to figure out where I'm going wrong with this implementation. Essentially, errors can occur for various reasons, but I only want to display the alert message once every fifteen minutes. For example, if an error occurs at 7:02AM, it should trigger the alert. However, if another error occurs at 7:10AM, it should not display. But if an error happens after 8:02AM, then the alert should show again and the cycle continues.
Currently, the alert message is being displayed regardless of what I try to fix it.
lastTimeErrorWasDisplayed = 0;
displayError() {
let vm : any = this;
// get the current time
const timeNow = new Date().getTime();
const hoursToWaitBeforeNextToastr = 1;
const difference = timeNow - this.lastTimeErrorWasDisplayed
var hours = (difference / (1000 * 60 * 60));
// should we show toastr now?
if (
hours > hoursToWaitBeforeNextToastr
) {
this.lastTimeErrorWasDisplayed = timeNow;
setTimeout(() => {
vm.toastr.warning(
"error",""
);
});
}
}
The setTimeout is necessary due to the application requirements. Even without it, however, the error still shows up regardless of when it occurred or was last displayed.