简体   繁体   English

如何在 Pug 中删除一段时间后的错误警报

[英]How to delete an error alert after a time in Pug

guys.伙计们。 I'm still learning express, and I am using Pug to view my elements in the browser.我还在学习 express,我正在使用 Pug 在浏览器中查看我的元素。 I'm showing an error alert when a client tries to send a testimonial before to fill all information.当客户尝试在填写所有信息之前发送推荐时,我会显示错误警报。 The problem is those alerts stay on the screen and don't disappear.问题是那些警报停留在屏幕上并且不会消失。 I would like to make them disappear after a few seconds.我想让它们在几秒钟后消失。

This is the code I'm using to send the errors to testimoniales.pug file.这是我用来将错误发送到 testimonales.pug 文件的代码。

const errores = [];

    if(name.trim() === ''){
        errores.push({message : 'The name is empty'});
    }
    
    if(mail.trim() === ''){
        errores.push({message : 'The mail is empty'});
    }
    
    if(message.trim() === ''){
        errores.push({message  : 'The message is empty'});
    }
    

   * if(errores.length > 0){

        //consult the testimonials that already exis
        const testimoniales = await Testimonial.findAll();

        //show the view with errors
        res.render('testimoniales', {
            page: 'Testimoniales', 
            errores,
            name, 
            main,
            message,
            testimoniales
        });*
    }else{
        //Save in the DATABASE 
        try {
            await Testimonial.create({
                name, 
                mail,
                message
            });

            res.redirect('/testimoniales');

        } catch (error) {
            console.log(error);
        }
    }

This is the code to show the errors in my testimoniales.pug file.这是在我的 testimonales.pug 文件中显示错误的代码。

 if(errores)
                    each error in errores
                        .alert.col.alert-danger.text-center= error.mensaje

So, all I want is to eliminate the alerts after 3 or 5 seconds.所以,我只想在 3 或 5 秒后消除警报。

You would need some client-side Javascript.您将需要一些客户端 Javascript。

Something like:就像是:

if (errores)
  #errors
    each error in errores
      .alert.col.alert-danger.text-center=error.mensaje

And add, maybe after </body> ,:并添加,也许在</body>之后:

if (errores)
  script.
    setTimeout(()=>
    document.getElementById("errors").style.display="none", 5000);

I'm still learning express我还在学习express

Learning Express is a toy store. Learning Express 是一家玩具店。 You're a store?你是商店?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM