我有一个 jQuery 脚本,它加载一个包含表格的 PHP 文件。悬停时是否可以暂停表格加载?也可以设置时间间隔在第一次加载页面时立即加载文件,然后每 3 秒加载一次?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#results2').load('includes/chatlog.php');
}, 3000); // refresh rate in milliseconds.
});
// ]]>
</script>
最佳答案
要在 table
元素悬停时暂停计时器,您可以使用 clearInterval()
。然后,当鼠标离开 table
时,您需要再次开始间隔。此外,要在页面加载时立即请求数据,您可以将 load()
调用提取到它自己的函数中,该函数会立即执行。试试这个:
function getData() {
$('#results2').load('includes/chatlog.php');
}
var timer;
$(document).ready(function() {
$.ajaxSetup({ cache: false });
timer = setInterval(getData, 3000); // refresh every 3 seconds
getData(); // get data on load
$('#results2').on({
mouseenter: function() {
clearInterval(timer);
},
mouseleave: function() {
timer = setInterval(getData, 3000);
}
});
});
关于javascript - 悬停时停止 jQuery 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35768600/