我有一个 jQuery 代码,
<script type="text/javascript">
$.idleTimeout('#idletimeout', '#idletimeout a', {
idleAfter: 3,
pollingInterval: 2,
keepAliveURL: 'keepalive.php',
serverResponseEquals: 'OK',
onTimeout: function(){
$(this).slideUp();
window.location = "timeout.htm";
},
onIdle: function(){
$(this).slideDown(); // show the warning bar
},
onCountdown: function( counter ){
$(this).find("span").html( counter ); // update the counter
},
onResume: function(){
$(this).slideUp(); // hide the warning bar
}
});
//
</script>
现在,如果我调用它,它就会隐藏 slider 。
function loadXMLDoc(message)
{
$.get("test1.php?data="+message, function(result){
$('#idletimeout').slideUp(); // hide
});
}
</script>
有没有办法在loadXMLDoc函数中调用onResume函数(在顶部)? 谢谢。
最佳答案
查看该插件,它将恢复函数绑定(bind)到传入的第二个参数的单击处理程序(在您的情况下为“#idletimeout a”)。如果您想手动重置计时器,您应该可以这样做:
$('#idletimeout a').click();
这将触发 onResume
函数,该函数将向上滑动 div 并重置计时器。
作为引用,我只是查看了插件的源代码,看看发生了什么。您可以在此处查看相关部分。这是单击恢复元素(第二个参数)时发生的情况:
// bind continue link
this.resume.bind("click", function(e){
e.preventDefault();
win.clearInterval(self.countdown); // stop the countdown
self.countdownOpen = false; // stop countdown
self._startTimer(); // start up the timer again
self._keepAlive( false ); // ping server
options.onResume.call( self.warning ); // call the resume callback
});
如果您只是想直接调用它(并且不发生任何重置计时器的内部操作),您也可以这样做:
var idlePlugin = $.idleTimeout('#idletimeout', '#idletimeout a', { ...
idlePlugin.options.onResume.call();
但请记住,这不会重置计时器,它只会直接调用 onResume 函数。重置计时器的唯一方法是调用点击处理程序,因为这是定义此功能的地方。
关于javascript - 在 jQuery 中调用此函数时感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10458761/