My question is can i give a time limit to it? For example after 10sec it disappears on its own.
If you are referring to the Notification API, then those notifications should (on most browsers) automatically close themselves after about 4 or so seconds. This is stated at the resource below in the section titled “Closing Notifications”
There they also advise that you should not dismiss notifications automatically because you want the user to always be in control of when the notifications are closed. Using the close() function also may have the undesired effect of removing the notification before the user sees it and also remove it from further interaction. Something the user may not want.
setTimeout(removeNotification(bar), 10 * 1000); // doesn't work
The reason why that doesn’t work is that the removeNotification() function gets called right away. What’s needed instead is to pass a function to setTimeout, so that that function can then be called at a later time.
We need a function that can remember that element, which involves something called closure. That’s where an outer function returns an inner function, resulting in the inner one retaining knowledge of variables from the outer one.
That’s achieved by using a callback function. A callback function is best used for that, as that tell us that invoking the removeNotificationCallback() function results in another function.
Thanks for the help!
If i place the “setTimeout(function removeNotificationCallback(bar), 10 * 1000);” in my code, i doesnt run, and i have no idea why. I placed it after my last line of my notify function.