简体   繁体   English

单击 div 时停止自动刷新

[英]stop auto refresh when clicked on a div

i would Like to stop auto refresh when i click on a specfic div this is how my code looks like at the moment我想在单击特定 div 时停止自动刷新,这就是我的代码目前的样子

jquery.js jquery.js

var documentInterval;
$(document).ready(function(){
  documentInterval setInterval(function(){
      $('#show_here').load('fetch_something.php')
  }, 3000);
});

$('#stop_auto_refresh').click(function(){
  clearInterval(documentInterval);
});

index.php索引.php

<div id="show_here"></div>
<div id="stop_auto_refresh" onclick="documentInterval()">Click To Stop Auto Refresh</div>

I would like to stop auto refresh when click on the id stop_auto_refresh is this possible ?我想在单击id stop_auto_refresh时停止自动刷新这可能吗?

Intervals are cleared as using the clearInterval(intervalReference) .使用clearInterval(intervalReference)清除clearInterval(intervalReference) You can add a click handler using the .click() jQuery api since you are using jQuery.您可以使用.click() jQuery api 添加点击处理程序,因为您使用的是 jQuery。

var documentInterval;

$(document).ready(function() {
   documentInterval = setInterval(function(){
      $('#show_here').load('fetch_something.php')
   }, 3000);

   $('#stop_auto_refresh').click(function(){
     clearInterval(documentInterval) 
   })

});    

您需要将间隔函数设置为 var,然后您可以使用 onclick 事件触发另一个包含 clearInterval(varObject); 的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM