doukuangxun5382 2014-01-31 23:47
浏览 21
已采纳

ClearInterval()不工作任何人都知道为什么?

I'm updating the user posts via this code:

var auto_refresh = setInterval(function() 
{
    $('.contenido-publicaciones')
        .load("refresh_p.php?search_user="+search_user).fadeIn();
}, 2500 ); //Which is working.

1: <div class="contenido-publicaciones">, all the <div class="this-is-a-post"> div's from refresh.p.php load here every 2,5 seconds.

2: I have a textarea tag inside for each <div class="this-is-a-post"> that refreshes from refresh_p.php.

3: When <div class="contenido-publicaciones"> refreshes, it also refresh the textarea and prevent the user for typing.

4: All of the textareas have the class b_green

I want to clearInterval(auto_refresh) when the user clicks a textarea with the class b_green so another user can send a comment to that post.

I've tried the following:

$(".b_green").click(function(){ clearInterval(auto_refresh); }); // Didn't work, <div class="contenido-publicaciones"> keep refreshing.

$("textarea").click(function(){ clearInterval(auto_refresh); }); // Works only when i click a textarea that i have on the top of the page to send posts, but doesn't work when i click a textarea with the class b_green.

$(this).click(function(){ clearInterval(auto_refresh); }); //This works if i click on b_green textareas, but stop the Interval on every click that the user does on the page, I only want to stop on b_green textareas click.

Is there any error on my code? Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • doqdcks3948 2014-01-31 23:56
    关注

    The clicks do not fire because at the time you are binding them there are no textareas or the ones that there are are latter replaced with new ones. These new textareas do not have the onclick handler attached.

    You could do either of these:

    1. Add onclick="clearInterval(auto_refresh);" to the code of the textarea that backend sends and get something like:

      <textarea onclick="clearInterval(auto_refresh);"></textarea>
      
    2. Use on to bind the click handler to a higher element that remains on the page like so:

      $(".contenido-publicaciones").on('click', 'textarea', function(){
          clearInterval(auto_refresh);
      });
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?