[英]How can I get multiple Javascript setInterval functions to play nicely together on the same page?
I have written a few custom animation functions as jQuery plugins using a setInterval()
for the loop. 我使用
setInterval()
循环编写了一些自定义动画功能作为jQuery插件。
The functions seem to work fine by themselves but when I try to include more than one of them in the same page the timings and effects get all messed up and don't render or complete properly! 这些功能本身似乎可以正常工作,但是当我尝试在同一页面中包含多个功能时,时间和效果会变得一团糟,无法正确渲染或完成!
I have done some scouting around on stackOverflow and google and from what I can gather the problem is that setInterval
acts as a 'global' method / property of the window
object and so when I have more than one on the page they end up overwriting each other! 我已经在stackOverflow和google上进行了一些探索,从我可以收集到的问题是
setInterval
充当window
对象的“全局”方法/属性,因此当我在页面上有多个时,它们最终将覆盖每个对象其他!
Is this correct? 这个对吗? And if so what is the solution?
如果可以的话,解决方案是什么? What does jQuery / plugins do to manage their own unique interval functions?
jQuery /插件如何管理自己的唯一间隔函数?
I was wondering if it's possible to set a 'local' setInterval
as a unique property to each of my plugin objects? 我想知道是否可以将“本地”
setInterval
设置为每个插件对象的唯一属性?
-- OR -- - 要么 -
If I have to create a function to manage multiple animation calls from various different plugins / functions to juggle them all with one global setInterval
call -- But I have no idea how to do this! 如果我必须创建一个函数来管理来自各种不同插件/函数的多个动画调用,以通过一个全局
setInterval
调用来处理所有动画,但是我不知道该怎么做!
Can anyone help please? 有人可以帮忙吗?
----edit---- - - 编辑 - -
(function($){
$.fn.scroller = function(options) {
var defaults = {
direction: 'left',
distance: '',
duration: 2000,
interval: 2000,
type: 'linear',
startPos: '0'
};
var options = $.extend(defaults, options);
return this.each(function() {
parent = $(this).children('.scroller');
$this = parent.children().first();
// console.log(parent);
// console.log($this);
var o = options;
var m = '';
if(o.direction === 'left') m = 'marginLeft';
if(o.direction === 'right') m = 'marginRight';
if(o.direction === 'up') m = 'marginTop';
if(o.direction === 'down') m = 'marginBottom';
// console.log('Distance: ' + o.distance);
// console.log('Duration: ' + o.duration);
// console.log('Type: ' + o.type);
// console.log('Start Pos: ' + o.startPos);
// console.log('Interval: ' + o.interval);
setInterval(function(){
$this.animate({
m : o.distance
},
o.duration,
o.type,
function() { // OnComplete
$this.stop().css(m, o.startPos).appendTo(parent);
});
}, o.duration+o.interval);
});
};
})(jQuery);
I think I see the problem. 我想我看到了问题。 You define $this as a global variable.
您将$ this定义为全局变量。 As such, calls to setInterval will all use the same $this variable.
这样,对setInterval的调用将全部使用相同的$ this变量。 Try
var $this = ...
. 尝试
var $this = ...
Same for parent
. parent
。 Also note that in the callback to $this.animate
, "this" should refer to the dom element that was animated, so you could do $(this) to get a jQuery object for it. 另请注意,在
$this.animate
的回调中,“ this”应引用已动画化的dom元素,因此您可以执行$(this)为其获取jQuery对象。
I think your problem might be that you loose context with setInterval. 我认为您的问题可能是您使用setInterval松散了上下文。 The
this
object may not be what you expect it to be. 在
this
对象可能不是你希望它是什么。 If you expect a variable to be present (because it was defined near where you call setInterval), it probably won't be. 如果您希望存在一个变量(因为它是在调用setInterval的位置附近定义的),则可能不会存在。
Closures might help you. 关闭可能会帮助您。 Try
尝试
setInterval(function(){
//code here
});
Rather than 而不是
setInterval(myPreDefinedFunction);
Also, try to make sure you are using locally defined variables in your setInterval callback rather than global ones: 另外,请尝试确保在setInterval回调中使用本地定义的变量,而不是全局变量:
var myVar = 5;
rather than 而不是
myVar = 5;
If you used the second method, you would end up sharing the variable between callbacks. 如果使用第二种方法,最终将在回调之间共享变量。
it is true that setInterval is global, but each call to setInterval returns a unique ID that you can use to reference it, ie for clearInterval(). 确实,setInterval是全局的,但是每次调用setInterval都会返回一个唯一的ID,您可以使用该ID来引用它,即clearInterval()。 Each plugin requiring it's own interval should be assigned it's own variable to contain the ID for referencing.
每个需要使用自己的时间间隔的插件都应该分配给自己的变量,以包含要引用的ID。 Perhaps it's coming down to how you are managing each of these individually.
也许这取决于您如何分别管理每一个。 Do you have any code to show us?
您有任何代码可以向我们展示吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.