0

I've currently got a set of javascript functions that are used to set intervals and clear them. The issue is; the intervals are stored in an array (intervals[])

config: {
    settings: {
        timezone: 'Australia/Perth,',
        base_url: location.protocol + '//' + location.hostname + '' + ((location.pathname) ? location.pathname : ''),
        api_url: '/api/'
    },
    intervals: []
}

There are two functions that work with that array: The first being the interval setter:

interval_set: function(time, name, callback) {
        this.config.intervals[name] = setInterval(callback, time);
    }

and the second being the interval clearer

interval_clear: function(name) {
        if (this.config.intervals[name]) {
            clearInterval(this.config.intervals[name]);
            this.debug('Interval: "' + name + '" cleared.', 1);
        } else {
            this.debug('No interval: "' + name + '" found.', 1);
        }
    }

Now the issue is I need to remove said interval from the intervals array, although as you can see - it isn't set with a specific key.

The interval array looks like this:

Array[0]
    derp: 1

I've tried using splice() but that doesn't seem to work. What would you suggest I do? Should I store it in the array with an index?

Thanks :)

2
  • 2
    Array: numeric index; Object: string key. Looks like you mixed them up. Commented Apr 2, 2014 at 2:57
  • @Bergi Thanks for pointing that out, will fix my stupidity now :) Commented Apr 2, 2014 at 2:59

1 Answer 1

2

It sounds like what you want is just:

delete this.config.intervals[name];

Incidentally, you aren't really using intervals as an Array here, you're just using it like a regular Object. So it would be more correct for its starting value to be {} instead of [].

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I was looking for! I don't know why I didn't try this. Thanks! Edit: Will change it properly from object to array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.