Search code examples
javascriptjqueryinternet-explorerinternet-explorer-9

Javascript - Apply() breaking IE 9


I have this object:

var _intervals = {
    intervals: {},
    _add: function (fun, interval) {
        var newInterval = setInterval.apply(
            window,
            [fun, interval].concat([].slice.call(arguments, 2))
        );

        this.intervals[ newInterval ] = true;
        return newInterval;
    },
    _delete: function (id) {
        return clearInterval(this.intervals[id]);
    },
    _deleteAll: function () {
        var all = Object.keys(this.intervals), len = all.length;
        while (len-- > 0) {
            clearInterval(all.shift());
        }
    }
};

For some strange reasons when I remove the apply() function from the _add property IE9 works, if I leave it there IE9 stops working, but the property _add needs that apply() to work as supposed.

I researched for issues of apply() in IE9 but cannot find anything related.

Any help?

Thank you very much


Solution

  • IE9 doesn't support passing parameters to the callback through arguments of setInterval.

    So this isn't a problem of apply but of setInterval.

    You may use this instead:

    var args = [].slice.call(arguments, 2), newInterval = setInterval(function(){
        fun.apply(window, args);
    }, interval);