开发者

jquery dialog opening and closing don't work

开发者 https://www.devze.com 2023-01-02 22:16 出处:网络
i want to show dialog after n seconds and hide it after m seconds but it don\'t work for me! $(document).ready(function () {

i want to show dialog after n seconds and hide it after m seconds but it don't work for me!

 $(document).ready(function () {
    var advanced = $("div#advanced");
    $(advanced).dialog({ autoOpen: false,
        modal: true,
        buttons: { "Try it now": f开发者_Python百科unction () { window.location = 'myURL'; },
            "No thank's": function () { $(this).dialog("close"); }
        },
        show: 'fade',
        width: 350,
        height: 130,
        draggable: false,
        resizable: false
    });
    window.setTimeout(function () {
        $(advanced).dialog("open");
    }, n);
    window.setTimeout(function () {
        $(advanced).dialog("close");
    }, m);});


Move the setTimeout that fires the dialog close into the callback for the timer that opens the dialog. You probably also want to clear the timer when the dialog closes.

 $(function () {
    var advanced = $("div#advanced");
    advanced.dialog({ autoOpen: false,
        modal: true,
        buttons: { "Try it now": function () {
                      window.location = 'myURL';
                    },
            "No thank's": function () {
                      $(this).dialog("close");;
                    }
        },
        close: clearTimer,
        show: 'fade',
        width: 350,
        height: 130,
        draggable: false,
        resizable: false
    });
    var closeTimer = null;

    setTimeout(function () {
        advanced.dialog("open");
        closeTimer = setTimeout( function() {
            closeTimer = null;
            advanced.dialog("close");
        }, m );
    }, n);

    function clearTimer() {
        if (closeTimer) {
           clearTimeout(closeTimer);
           closeTimer = null;
        }
    }

});


Try changing your code a bit, like this:

$(document).ready(function () {
   var advanced = $("div#advanced");
   advanced.dialog({ autoOpen: false,
       modal: true,
       buttons: { 
          "Try it now": function () { window.location = 'myURL'; },
          "No thank's": function () { $(this).dialog("close"); }
       },
       show: 'fade',
       width: 350,
       height: 130,
       draggable: false,
       resizable: false
   });
   setTimeout(function () {
       advanced.dialog("open");
   }, n);
   setTimeout(function () {
       advanced.dialog("close");
   }, m);
});

As Pointy points out in comments, you're cloning the advanced element by wrapping it in $() again, meaning that the element you're creating the dialog on and the element you're trying to open it on are separate clones, neither the original. Just use advanced directly like I have above, it's already a jQuery object :)

0

精彩评论

暂无评论...
验证码 换一张
取 消