我有以下代码。我想创建一个复选框,在选中时激活此代码,并在取消选中时停用代码。有人能告诉我怎么样?
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_tweets').load('record_count.php').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
<body>
<div id="load_tweets"> </div>
</body>
</script>
答案 0 :(得分:2)
setInterval(function () {
if ( $("#myCheckbox").is(":checked") ) {
$('#load_tweets').load('record_count.php').fadeIn("slow");
}
}, 10000); // refresh every 10000 milliseconds
答案 1 :(得分:2)
var refresh = function() { $('#load_tweets').load('record_count.php').fadeIn("slow"); };
refresh();
var auto_refresh = null;
$('#the-checkbox').change(function(ev) {
if (auto_refresh) clearInterval(auto_refresh);
auto_refresh = $(this).is(':checked') ? setInterval(refresh, 10000) : null;
});