How can I create an Alert in JavaScript that is seen for 2 seconds and then closes automatically (only) without the user clicking accept?
How can I create an Alert in JavaScript that is seen for 2 seconds and then closes automatically (only) without the user clicking accept?
I present to you
Toastr
, it is a libraryJavaScript
that generates notifications in a " modern " way:I do it with the setTimeout function. In the following way.
I see that you want to open and close the message, with Javascript this cannot be done, closing the message has to be done manually.
As another option is to show a window with the message and close it automatically after 2 seconds (2000 ms).
The caller of the function is the setTimeOut() method :
Another option that can help you is
JQuery jGrowl
I leave you an example with a duration of 2 seconds:
if you require something more "modern" I recommend
toastr
I like @fredyfx's solution but if you care about load speed and resources used by your app you can implement something similar in less bytes by putting only the functions you are going to use.
This is an example, of course you can adjust the styles to the aesthetics of your application and you can add more features. This is a basis for writing your own component without having to load large libraries that result in heavy applications.
With the standard javascript alert I doubt you can do it, but if you use an alert implemented from jquery as being
jqAlert
You will see in the example that to implement a
timer
, for example the one in the titleAlert Dialogs
definesyou will see
timer:5000
after that time it closes itself