我正在尝试使用超大 jquery 插件(此处)中底部的代码(来自此处)。这是因为我需要一个更准确的计时器,因为我正在同步图像和声音。
本质上,超大有以下几行:
vars.slideshow_interval = setInterval(base.nextSlide, base.options.slide_interval);
和
clearInterval(vars.slideshow_interval);
我需要做类似的事情:
vars.slideshow_interval = accurateInterval(base.nextSlide, base.options.slide_interval);
和
vars.slideshow_interval.clear();
问题我的 javascript 技能有些有限,我什至不知道以下函数的术语/描述,我想它需要采用 base.accurateInterval = function(time fn).. 的形式而不是按原样包装因为我怀疑问题与在错误的上下文中使用 (this) 有关。
我尝试按原样使用代码,当我调用 clear() 时,我得到“不是函数错误”。
请问我怎样才能让它工作?任何指针,非常感谢。谢谢你。
(function () {
window.accurateInterval = function (time, fn) {
var cancel, nextAt, timeout, wrapper, _ref;
nextAt = new Date().getTime() + time;
timeout = null;
if (typeof time === 'function') _ref = [time, fn], fn = _ref[0], time = _ref[1];
wrapper = function () {
nextAt += time;
timeout = setTimeout(wrapper, nextAt - new Date().getTime());
return fn();
};
cancel = function () {
return clearTimeout(timeout);
};
timeout = setTimeout(wrapper, nextAt - new Date().getTime());
return {
cancel: cancel
};
};
}).call(this);