bootstrap modal close after 4 seconds or user click

67,726

Solution 1

When calling setTimeout(), wrap your command in an anonymous function. Otherwise the command will be executed immediately.

setTimeout(function() {$('#forgot-form').modal('hide');}, 4000);

Solution 2

setTimeout(function(){
  $('#Modal').modal('hide')
}, 4000);

//where modal's id is 'Modal'

Solution 3

The following code is used for hiding the model on an onClick event. Use the classname for the onClick listener and the modal id as the selector to hide.

$('.class_name').on('click',function(){
    $('#modal_id').modal('hide');
});
Share:
67,726

Related videos on Youtube

Alex
Author by

Alex

Enjoy coding in my free time and helping others out.

Updated on July 07, 2022

Comments

  • Alex
    Alex almost 2 years

    How would I set a timeout for a bootstrap modal? After getting the ajax data back that the message returned by php contains the term success, I want to give the user the option to close the window. However, I also just want to have a 4 second count down. Currently the second the success message comes back the modal hides itself.

    $('#forgotform').submit(function (e) {
        "use strict";
        e.preventDefault();
        $('#forgotsubmit').button('loading');
        var post = $('#forgotform').serialize();
        var action = $('#forgotform').attr('action');
        $("#message").slideUp(350, function () {
            $('#message').hide();
            $.post(action, post, function (data) {
                $('#message').html(data);
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#usernamemail').focus();
                if (data.match('success') !== null) {
                    $('#forgotform').slideUp('slow');
                    $('#forgotsubmit').button('complete');
                    $('#forgotsubmit').click(function (eb) {
                        eb.preventDefault();
                        $('#forgot-form').modal('hide');
                    });
                    setTimeout($('#forgot-form').modal('hide'), 10000);
                } else {
                    $('#forgotsubmit').button('reset');
                }
            });
        });
    });
    
    • JofryHS
      JofryHS over 10 years
      What happens when you comment out the setTimeout? Does it automatically closes itself?
  • Maciej Jureczko
    Maciej Jureczko over 6 years
    Your answer does not bring anything new to the problem, which was solved 4 years ago.
  • Eyk Rehbein
    Eyk Rehbein over 5 years
    Hi there. I think this wasn't quite the correct answer for what was asked
  • Subhash Patel
    Subhash Patel over 5 years
    Hi @eykhagen Alex also want user click and hide modal.
  • ebentil
    ebentil about 5 years
    You left out the time
  • Subhash Patel
    Subhash Patel about 5 years
    @ebentil You read my note above it's only for onclick event.
  • ORHAN ERDAY
    ORHAN ERDAY over 3 years
    This actually a wrong answer but %100 I need this.