我正在使用一个 jquery 对话框,该对话框使用 iframe 让用户知道会话即将到期。我想用倒计时每秒更新这个对话框,因此我想缓存对倒计时元素的引用,这样我们就不必每秒查询 DOM。
我尝试过很多不同的方法,但我似乎无法让它发挥作用。
$(documnet).ready(function() {
$("<iframe src='../active_timer.html' id='iframeDialog' />").dialog({
autoOpen: false,
title: "Your session is about to expire!",
modal: true,
width: 400,
height: 200,
closeOnEscape: false,
draggable: false,
resizable: false,
buttons: {
"Yes, Keep Working": function(){
$(this).dialog("close");
},
"No, Logoff": function(){
//log user out
}
}
});
// cache a reference to the countdown element.
// these below don't cache the reference I have tried them all.
var $countdown = $("#iframeDialog").contents().find("#dialog-countdown");
var $countdown = $("#dialog-countdown", window.child);
var $countdown = $("#dialog-countdown", $('iframe').get(0).contentDocument);
//this is where I update the #dialog-countdown every second after the reference is made.
});
active_timer.html:
<div id="dialog" style="overflow:hidden;">
<p>
<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 25px 0;"></span>
You will be logged off in <span id="dialog-countdown"></span> seconds.
</p>
<p>Do you want to continue your session?</p>
</div>