我希望在n秒后显示对话框,并在m秒后隐藏它,但它对我不起作用!
$(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);});
答案 0 :(得分:1)
尝试稍微更改一下代码,如下所示:
$(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);
});
正如Pointy在评论中指出的那样,您正在克隆advanced
元素by wrapping it in $()
again,这意味着您正在创建对话框的元素以及您的元素重新尝试打开它是单独的克隆,既不是原始克隆。就像我上面那样直接使用advanced
,它已经是一个jQuery对象了:)
答案 1 :(得分:1)
将触发对话框的setTimeout移动到打开对话框的计时器的回调中。您可能还希望在对话框关闭时清除计时器。
$(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;
}
}
});