How to delay display of Bootstrap 3 modal after click

前端 未结 4 1241
情歌与酒
情歌与酒 2020-12-18 04:16

I\'m trying to delay the display of a Bootstrap modal after the user has clicked on a trigger button:

相关标签:
4条回答
  • 2020-12-18 04:50
    window.setTimeout(function() {
       $('#myModal').modal('show');
    }, 5000)
    

    For demo I used 5000 mini seconds. You can used it as per your need...

    0 讨论(0)
  • 2020-12-18 04:57

    Use .on() to hook into the event:

    var isFirstShowCall = false;
    
    $('#myModal').on('show.bs.modal', function (e) {
        isFirstShowCall = !isFirstShowCall; // Prevents an endless recursive call
        if(isFirstShowCall) {
                e.preventDefault(); // Prevent immediate opening
                window.setTimeout(function(){
                    $('#myModal').modal('show');
                }, 3000)
        }
    });
    

    http://jsfiddle.net/G9XeA/

    0 讨论(0)
  • 2020-12-18 05:02

    If any of those above method does't work, give this method a try. It will work perfectly.

    $(window).ready(function() {
       setTimeout(function(){
           $('#myModal').modal('show');
       }, 2000);
    });
    
    0 讨论(0)
  • 2020-12-18 05:05

    You can delay the click of the trigger button, then activate the modal directly:

    $('[data-toggle=modal]').on('click', function (e) {
        var $target = $($(this).data('target'));
        $target.data('triggered',true);
        setTimeout(function() {
            if ($target.data('triggered')) {
                $target.modal('show')
                    .data('triggered',false); // prevents multiple clicks from reopening
            };
        }, 3000); // milliseconds
        return false;
    });
    

    http://jsfiddle.net/mblase75/H6UM4/

    0 讨论(0)
提交回复
热议问题