最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove class after 3 seconds - Stack Overflow

programmeradmin2浏览0评论

I'd like to put a timeout function to remove these two classes, but I have no idea how to do that. Can anyone help me how to include a timeout here? Thanks in advance.

.done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text('Message sent!');

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
            //$('#budget').val('');
        })

I'd like to put a timeout function to remove these two classes, but I have no idea how to do that. Can anyone help me how to include a timeout here? Thanks in advance.

.done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text('Message sent!');

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
            //$('#budget').val('');
        })
Share Improve this question asked Oct 16, 2014 at 16:41 Lucas HaasLucas Haas 3411 gold badge2 silver badges15 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 24

maybe something like...

 setTimeout(function(){
            $(formMessages).removeClass('error');
            //....and whatever else you need to do
    }, 3000);

Use queue method (https://api.jquery.com/queue/) to queue your function calls in combination with delay.

$(formMessages)
  .addClass('error')
  .delay(3000)
  .queue(function(next){
    $(this).removeClass('error');
    next();
  })

Using Angular js you can use time function like this:

setTimeout(function(){
            var myE2 = angular.element( document.querySelector( '#compaignhiglighted' ) );
            myE2.removeClass('compaignhiglighted');
          }, 3000);

Using jquery...:

$(formMessages)
    .delay(3000) // its like settimeout
    .removeClass('error');

Reference: http://api.jquery.com/delay/

发布评论

评论列表(0)

  1. 暂无评论