我目前有一组javascript函数用于设置间隔并清除它们。
问题是;区间存储在一个数组中( intervals[]
)
config: {
settings: {
timezone: 'Australia/Perth,',
base_url: location.protocol + '//' + location.hostname + '' + ((location.pathname) ? location.pathname : ''),
api_url: '/api/'
},
intervals: []
}
有两个与该数组一起使用的函数: 第一个是区间设定器:
interval_set: function(time, name, callback) {
this.config.intervals[name] = setInterval(callback, time);
}
,第二个是间隔更清晰
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);
}
}
现在问题是我需要从区间数组中删除所述区间,尽管如您所见 - 它没有使用特定键设置。
区间数组如下所示:
Array[0]
derp: 1
我尝试过使用splice()
,但这似乎不起作用。你有什么建议我这样做?我应该将它存储在带索引的数组中吗?
谢谢:)
答案 0 :(得分:2)
这听起来像你想要的只是:
delete this.config.intervals[name];
顺便说一句,您并没有真正在intervals
使用Array
,而是像常规Object
一样使用它。所以它的起始值更为正确{}而不是[]。