我并不完全清楚,但如果我理解了你的问题,这应该可以解决问题(这里是小提琴):
$(document).ready(function () {
var colorSets = [],
body = $(document.body),
colorSetsIndex = 0, //goes from 0 to the max length of colorSets
colorIndex = 0, //goes from 0 to the max length of the current colorSet
started = false,
intervalId;
//add all the sets you want
colorSets.push(["blue", "green", "yellow"]);
colorSets.push(["grey", "red", "purple"]);
$(document).on('click', 'button', function () {
if (started) return;
started = true;
intervalId = setInterval(function () {
var currentSet = colorSets[colorSetsIndex];
body.css('background', currentSet[colorIndex]);
colorIndex += 1;
if (colorIndex === currentSet.length) {
colorSetsIndex++;
colorIndex = 0;
}
if (colorSetsIndex === colorSets.length) {
var restart = confirm('Restart?');
if (!restart) {
clearInterval(intervalId);
$("button").off('click');
return;
}
colorSetsIndex = 0;
colorIndex = 0;
}
}, 500);
});
});