jQuery - 多个 setInterval 冲突
我是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用单个
setInterval
。您可以这样整理代码:Use a single
setInterval
. You could tidy up the code thus:如果您已经使用 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)。将上面的所有代码替换为:
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:
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.