views:

80

answers:

2

hi, 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": function () { 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);});
+1  A: 

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 :)

Nick Craver
@Nike ;-) thank's again small hero, your location is paradise(بهشت در قرآن کریم)
Sadegh
@Nick - I don't think it actually makes a difference that it is cloning the jQuery element in this case since the dialog reuses the underlying DOM elements. If the widget maintained some state outside the DOM, then it probably would, but recreating the dialog widget shouldn't matter. Doesn't seem to in my testing anyway. I do agree that once you've created the element you should simply use it but I don't think that is causing the problem.
tvanfosson
+1  A: 

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;
        }
    }

});
tvanfosson
ok your approach word's fine but i want an approach like my own ;)
Sadegh
@Sadegh -- you don't need to close the window unless it's open. Since these are dependent events, the code for closing *should* be dependent on the dialog being opened. You set them up as independent events, and while it would work if you changed the expiration times so that the second takes longer than the first, it is dependent on the numbers being right. My way doesn't start the close timer until the dialog is opened, then it fires `m` ms later to close it unless it has already been closed by the user. Setting up the close timer in the dialog open even might even be better/more robust.
tvanfosson
Sorry I'm understand your means as bad/not good solution, in fact I'm don't understand your means first. thank' for your more in depth Explanation
Sadegh