Howdy guys
I lately began utilizing javascript, and that i made 2 buttons, which if pressed, then i get a push notification on my cellphone, and that i get a notify too ( Request has been despatched, OK or ERROR ), however its there till I click on X, to shut it.
My query is am i able to give a time restrict to it? For instance after 10sec it disappears by itself.
Thanks upfront
In case you are referring to the Notification API, then these notifications ought to (on most browsers) robotically shut themselves after about 4 or so seconds. That is said on the useful resource under within the part titled “Closing Notifications”
There additionally they advise that you shouldn’t dismiss notifications robotically since you need the person to all the time be answerable for when the notifications are closed. Utilizing the shut()
operate additionally could have the undesired impact of eradicating the notification earlier than the person sees it and likewise take away it from additional interplay. One thing the person could not need.
I hope that is what you had been referring to.
hey!
No, im not referring to API notifications.
I meant on my web site, i ship a bit of notify, when i press a button, it says “request has been despatched” then if its all okay then “OK”
I can ship you my code if you happen to like
.
@norbertkoteles10 Reasonably than ship might you share a hyperlink or a pattern of the code right here?
That manner others can contribute. It might even be useful to different members who run into the identical difficulty.
Sure there actually is a manner.
Once you present the notification, use setTimeout with a delay of 10 seconds, the place removeNotification is a operate.
setTimeout(removeNotification, 10 * 1000);
Or, if in case you have a number of notifications then you should use a operate to provide totally different data, for instance:
setTimeout(operate removeRequestNotification()
removeNotification("#request");
, 10 * 1000);
operate notify(msg, sort)
// msg : notification message
// sort : notification sort [info
Hey, Where do i put that?
Well you are using an element called bar.
It’s not possible to do the following:
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.
setTimeout(function removeNotificationCallback(bar), 10 * 1000);
That removeNotificationCallback function needs to return a function. It is inside of that returned function that we do things with bar, as the el variable.
function removeNotificationCallback(el)
return function removeNotification()
el.remove();
setTimeout(function removeNotificationCallback(bar), 10 * 1000);
That removeNotificationCallback function can be placed in your code outside of the notify() function.
The setTimeout line is best placed immediately after the last line in the notify function.