使用 jQuery 和选项卡以一定的时间间隔将内容重新加载到 div 中

发布于 2024-10-17 21:29:59 字数 2700 浏览 0 评论 0原文

我有几个选项卡,单击这些选项卡时,会使用 jquery 将数据加载到 div 中。我需要以 5 秒的间隔重新加载数据,但只需要来自特定选项卡的信息。所以,这就是我所拥有的...我想我需要在单击一个选项卡时启动 setInterval,然后在单击另一个选项卡时停止它。这是我的选项卡代码:

<script type="text/javascript">    
$(document).ready(function(){
  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     $("#actionlist").load("alcurrent.php");
     });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     $("#actionlist").load("alpending.php");
     });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     $("#actionlist").load("alpast.php");
     });
});
</script>

例如,当单击 #xicon1 时,我需要它将 alcurrent.php 加载到 #actionlist (我可以做到这一点),但我需要它每 5 秒重新加载一次。然后,当我单击另一个选项卡时,我需要该选项卡每 5 秒开始加载一次。因此,无论哪个选项卡处于活动状态,我都需要每 5 秒加载一次内容。

我尝试过 setInterval...但没有骰子。

谢谢。

编辑:

这就是我所做的,我认为它有效...

<script type="text/javascript">
$(document).ready(function(){
  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon1() {
     $("#actionlist").load("alcurrent.php");
     }
     setInterval(xicon1, 5000);
     });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon2() {
     $("#actionlist").html("<p>2</p>")
     }
     setInterval(xicon2, 5000);
     });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon3() {
     $("#actionlist").html("<p>3</p>")
     }
     setInterval(xicon3, 5000);
     });
});
</script>

编辑2:这可能会更漂亮一些:

<script type="text/javascript">
$(document).ready(function(){
  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon1() {
     $("#actionlist").load("alcurrent.php");
     };
     $(function() {
     setInterval(xicon1, 5000);
     };
  });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon2() {
     $("#actionlist").html("<p>2</p>")
     };
     setInterval(xicon2, 5000);
  });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon3() {
     $("#actionlist").html("<p>3</p>")
     };
     setInterval(xicon3, 5000);
  });
});
</script>

好吧,我只是想确定:当有人单击不同的选项卡时,它会停止另一个选项卡的内容每 5 秒刷新一次?

I had a few tabs that, when clicked, use jquery to load data into a div. I need the data to reload at a 5 second interval, but only the info from the particular tab. So, here's what I have... I think I need to start a setInterval when a tab is clicked, then stop it when another is clicked. Here's my code for the tabs:

<script type="text/javascript">    
$(document).ready(function(){
  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     $("#actionlist").load("alcurrent.php");
     });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     $("#actionlist").load("alpending.php");
     });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     $("#actionlist").load("alpast.php");
     });
});
</script>

So for example, when #xicon1 is clicked, I need it to load alcurrent.php into #actionlist (I can do this), but I need it to reload it every 5 seconds. Then when I click another tab, I need that one to start loading every 5 seconds. So, whichever tab is active, I need the content loaded every 5 seconds.

I've tried setInterval... but no dice.

Thanks.

Edit:

Here's what I've done and I think it works...

<script type="text/javascript">
$(document).ready(function(){
  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon1() {
     $("#actionlist").load("alcurrent.php");
     }
     setInterval(xicon1, 5000);
     });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon2() {
     $("#actionlist").html("<p>2</p>")
     }
     setInterval(xicon2, 5000);
     });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon3() {
     $("#actionlist").html("<p>3</p>")
     }
     setInterval(xicon3, 5000);
     });
});
</script>

Edit2: this might be a little prettier:

<script type="text/javascript">
$(document).ready(function(){
  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon1() {
     $("#actionlist").load("alcurrent.php");
     };
     $(function() {
     setInterval(xicon1, 5000);
     };
  });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon2() {
     $("#actionlist").html("<p>2</p>")
     };
     setInterval(xicon2, 5000);
  });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");
     function xicon3() {
     $("#actionlist").html("<p>3</p>")
     };
     setInterval(xicon3, 5000);
  });
});
</script>

Okay, I just want to be sure: When someone clicks on a different tab, its going to stop the other tab's content from refreshing every 5 seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮坠入山谷 2024-10-24 21:30:00

你的setInterval的语法是什么,你试过这样吗?

setInterval(function() {
      // Do something every 5 seconds
}, 5000);

What is the syntax of your setInterval, have you tried it like this?

setInterval(function() {
      // Do something every 5 seconds
}, 5000);
千寻… 2024-10-24 21:30:00

我认为您只需要在单击新选项卡时清除间隔并重置它,如下所示:

<script type="text/javascript">
$(document).ready(function(){
  var the_interval = 0;

  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");

     if (the_interval) {
        clearInterval(the_interval);
     }
     xicon1();  // I would call it here, to load newest data as soon as tab is clicked

     function xicon1() {
     $("#actionlist").load("alcurrent.php");
     };
     $(function() {
     the_interval = setInterval(xicon1, 5000);
     };
  });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");

     if (the_interval) {
        clearInterval(the_interval);
     }
     xicon2();  // I would call it here, to load newest data as soon as tab is clicked

     function xicon2() {
     $("#actionlist").html("<p>2</p>")
     };
     setInterval(xicon2, 5000);
  });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");

     if (the_interval) {
        clearInterval(the_interval);
     }
     xicon3();  // I would call it here, to load newest data as soon as tab is clicked

     function xicon3() {
     $("#actionlist").html("<p>3</p>")
     };
     setInterval(xicon3, 5000);
  });
});
</script>

希望这对您有用。

I think you just need to clear the interval when a new tab is clicked and reset it, like this:

<script type="text/javascript">
$(document).ready(function(){
  var the_interval = 0;

  $("#xicon1").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");

     if (the_interval) {
        clearInterval(the_interval);
     }
     xicon1();  // I would call it here, to load newest data as soon as tab is clicked

     function xicon1() {
     $("#actionlist").load("alcurrent.php");
     };
     $(function() {
     the_interval = setInterval(xicon1, 5000);
     };
  });
  $("#xicon2").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");

     if (the_interval) {
        clearInterval(the_interval);
     }
     xicon2();  // I would call it here, to load newest data as soon as tab is clicked

     function xicon2() {
     $("#actionlist").html("<p>2</p>")
     };
     setInterval(xicon2, 5000);
  });
  $("#xicon3").click(function(){
     $('a[name^="xicon"]').removeClass();
     $(this).addClass("active");

     if (the_interval) {
        clearInterval(the_interval);
     }
     xicon3();  // I would call it here, to load newest data as soon as tab is clicked

     function xicon3() {
     $("#actionlist").html("<p>3</p>")
     };
     setInterval(xicon3, 5000);
  });
});
</script>

Hope this works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文