Javascript 间隔/设置超时起始点

Javascript 间隔/设置超时起始点,javascript,animation,loops,settimeout,Javascript,Animation,Loops,Settimeout,我有几个循环动画(向上/向下),它们由以下函数定义 循环间隔 function Cycler(f) { if (!(this instanceof Cycler)) { // Force new return new Cycler(arguments); } // Unbox args if (f instanceof Function) { this.fns = Array.prototype.slice.call

我有几个循环动画(向上/向下),它们由以下函数定义

循环间隔

function Cycler(f) {
    if (!(this instanceof Cycler)) {
        // Force new
        return new Cycler(arguments);
    }
    // Unbox args
    if (f instanceof Function) {
        this.fns = Array.prototype.slice.call(arguments);
    } else if (f && f.length) {
        this.fns = Array.prototype.slice.call(f);
    } else {
        throw new Error('Invalid arguments supplied to Cycler constructor.');
    }
    this.pos = 0;
}

Cycler.prototype.start = function (interval) {
    var that = this;
    interval = interval || 1000;
    this.intervalId = setInterval(function () {
        that.fns[that.pos++]();
        that.pos %= that.fns.length;
    }, interval);
}
职能1(向上)

功能2(向下)

开始


动画现在从1000(间隔时间)+0/500/2000(设置超时)开始,但我希望它们从0、500和2000毫秒开始。有人能帮忙吗?

如果我理解正确,您实际上是说您希望间隔回调不仅绑定到间隔,而且在超时结束和设置间隔之间执行一次

这意味着修改
Cycler.prototype.start

Cycler.prototype.start = function (interval) {
    var that = this, int_callback;  //<-- added var
    interval = interval || 1000;
    this.intervalId = setInterval(int_callback = function () {
        that.fns[that.pos++]();
        that.pos %= that.fns.length;
    }, interval);
    int_callback(); //<-- added line - call the func immediately, once
}
Cycler.prototype.start=函数(间隔){
var=this,int_callback;//一个解决方案是:

Cycler.prototype.start = function (interval,executeImmediately) {
    var that = this;
    interval = interval || 1000;
    var driverFunction = function () {
        that.fns[that.pos++]();
        that.pos %= that.fns.length;
    }
    this.intervalId = setInterval(driverFunction , interval);
    if(executeImmediately) {
        driverFunction();
    }
}

这将使您的函数定义一次,您只需将其馈送到
setInterval
,并直接调用它。

我认为这将把
int\u回调
放入全局命名空间。可能想在其中抛出一个
var
。非常感谢-这对我来说很有效!只是一个小小的更改:必须声明int\u回调var单独使用:var that=this;var int_callback;这将与我当前的代码完全相同-它是一个本地函数。尝试它-在
循环之外无法访问。prototype.start
@Betsy:实际上,
int_callback
不必在单独的语句中声明。但是,关于是否应该声明它还有争议。您可以我有一些意见
        function c() { Cycler(peekTile, unpeekTile).start(); }
        setTimeout(c, 0);

        function c2() { Cycler(peekTile2, unpeekTile2).start(); }
        setTimeout(c2, 500);

        function c3() { Cycler(peekTile3, unpeekTile3).start(); }
        setTimeout(c3, 2000);
Cycler.prototype.start = function (interval) {
    var that = this, int_callback;  //<-- added var
    interval = interval || 1000;
    this.intervalId = setInterval(int_callback = function () {
        that.fns[that.pos++]();
        that.pos %= that.fns.length;
    }, interval);
    int_callback(); //<-- added line - call the func immediately, once
}
Cycler.prototype.start = function (interval,executeImmediately) {
    var that = this;
    interval = interval || 1000;
    var driverFunction = function () {
        that.fns[that.pos++]();
        that.pos %= that.fns.length;
    }
    this.intervalId = setInterval(driverFunction , interval);
    if(executeImmediately) {
        driverFunction();
    }
}