jQuery - 多个 setInterval 冲突

发布于 2024-10-16 11:29:22 字数 885 浏览 4 评论 0原文

我是一个 jQuery 新手,下面的每一个都可以单独工作,但在一起工作时会花时间。我做错了什么?对代码的任何改进也将不胜感激......它将用于轮换广告。

<!--- Header Rotator --->
<script type="text/javascript">
$(document).ready(function() {
    $("#header").load("header.cfm");
    var refreshHeader = setInterval(function() {
        $("#header").load("header.cfm");
    }, 10000);
});
</script>
<!--- Main Rotator --->
<script type="text/javascript">
$(document).ready(function() {
    $("#main").load("main.cfm");
    var refreshMain = setInterval(function() {
        $("#main").load("main.cfm");
    }, 5000);
});
</script>
<!--- Footer Rotator --->
<script type="text/javascript">
$(document).ready(function() {
    $("#footer").load("footer.cfm");
    var refreshFooter = setInterval(function() {
        $("#footer").load("footer.cfm");
    }, 2000);
});
</script>

I am a jQuery novice and each of the following work fine on their own, but get out of time when working together. What am I doing wrong? Any improvement on the code would be appreciated too... It is to be used to rotate advertising.

<!--- Header Rotator --->
<script type="text/javascript">
$(document).ready(function() {
    $("#header").load("header.cfm");
    var refreshHeader = setInterval(function() {
        $("#header").load("header.cfm");
    }, 10000);
});
</script>
<!--- Main Rotator --->
<script type="text/javascript">
$(document).ready(function() {
    $("#main").load("main.cfm");
    var refreshMain = setInterval(function() {
        $("#main").load("main.cfm");
    }, 5000);
});
</script>
<!--- Footer Rotator --->
<script type="text/javascript">
$(document).ready(function() {
    $("#footer").load("footer.cfm");
    var refreshFooter = setInterval(function() {
        $("#footer").load("footer.cfm");
    }, 2000);
});
</script>

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

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

发布评论

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

评论(2

云仙小弟 2024-10-23 11:29:22

使用单个setInterval。您可以这样整理代码:

$(document).ready(function() {
     refreshCounter = 0;
     $("#header").load("header.cfm").data({refresh: 10});
     $("#main"  ).load("main.cfm"  ).data({refresh:  5});
     $("#footer").load("footer.cfm").data({refresh:  2});
     var refreshHeader = setInterval(function() {
         refreshCounter++;
         $("#header, #main, #footer").each(function(){
             var $this   = $(this);
             var refresh = $this.data('refresh');
             if ((refreshCounter % refresh) == 0) {
                 var cfmFile = this.id + '.cfm';
                 $this.load(cfmFile);
             }
         });
     },
     1000);
}); 

Use a single setInterval. You could tidy up the code thus:

$(document).ready(function() {
     refreshCounter = 0;
     $("#header").load("header.cfm").data({refresh: 10});
     $("#main"  ).load("main.cfm"  ).data({refresh:  5});
     $("#footer").load("footer.cfm").data({refresh:  2});
     var refreshHeader = setInterval(function() {
         refreshCounter++;
         $("#header, #main, #footer").each(function(){
             var $this   = $(this);
             var refresh = $this.data('refresh');
             if ((refreshCounter % refresh) == 0) {
                 var cfmFile = this.id + '.cfm';
                 $this.load(cfmFile);
             }
         });
     },
     1000);
}); 
剪不断理还乱 2024-10-23 11:29:22

如果您已经使用 jQuery,请查看此插件 http://plugins.jquery.com/project/timers ,已知有多个 setintervterval 计时有时会出现一些问题。因此,如果您像 Isaac 建议的那样修复代码,但发现它仍然不起作用,请尝试以下操作:我不会制作多个计时器,而是制作 1 个以最低延迟触发的调度程序函数(在本例中为 2000),然后放置一个计数器每次增加 2000 并执行类似 if((counter % 2000) == 0){ action1code;}
if((计数器 % 6000) == 0) { action2code}
if((counter % 10000) == 0) { action3code}

希望这是清楚的,我现在不太敏锐:)

更新:好吧,如果没有你的代码,或者我自己写一大堆代码,我无法测试这个但我相信这可能对你有用(尽管我今天的注意力很低,所以我可能错过了一些明显的东西;D)。将上面的所有代码替换为:

 <script type="text/javascript">
 count = 10000; // this is so it will load everything the first time through.
 function timerFired () {
      if((count % 2000)==0){
           $("#footer").load("footer.cfm");
      }
      if ((count % 6000)==0) {
           $("#main").load("main.cfm");
      }
      if ((count % 10000)==0){
           $("#header").load("header.cfm");
           count =0;
      }
      count = count + 2000;
 }
 $(document).ready(function() {
     var refreshHeader = setInterval(timerFired(),2000);
  });
 </script>

PS 如果直接从浏览器访问,请不要忘记清除浏览器缓存并确保 .cfm 的更改符合您的预期。

If your using jQuery already check out this plugin http://plugins.jquery.com/project/timers , Having multiple setintervterval timings is known to have some issues sometimes. So if you fix your code like Isaac suggests and you find it still dosen't work try this: Instead of making multiple timers I would make 1 scheduler function that fires at your lowest delay(in this case 2000) and then put a counter that increments by 2000 each time and do something like if((counter % 2000) == 0){ action1code;}
if((counter % 6000) == 0) { action2code}
if((counter % 10000) == 0) { action3code}

Hope that's clear, I'm not to sharp at the moment :)

UPDATE: Well I can't test this without having either your code, or writing a whole bunch myself but I believe this might work for you (although my concentration today is pretty low so I may have missed something obvious ;D ). Replace all your code above with this:

 <script type="text/javascript">
 count = 10000; // this is so it will load everything the first time through.
 function timerFired () {
      if((count % 2000)==0){
           $("#footer").load("footer.cfm");
      }
      if ((count % 6000)==0) {
           $("#main").load("main.cfm");
      }
      if ((count % 10000)==0){
           $("#header").load("header.cfm");
           count =0;
      }
      count = count + 2000;
 }
 $(document).ready(function() {
     var refreshHeader = setInterval(timerFired(),2000);
  });
 </script>

P.S. Don't forget to clear your browsers cache and make sure your .cfm's are changing as you intended if accessed directly from the browser.

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