As the others have said, it's length, not lenght.
But no one seems to have addressed the second part of your question, so:
You don't need push to cycle through the values. All you need is an index:
var fondetcaption = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
var fondetcaptionIndex = 0;
// Call this when you click your button or whatever
function getNextBackground() {
if (fondetcaptionIndex >= fondetcaption.length) {
fondetcaptionIndex = 0;
}
return fondetcaption[fondetcaptionIndex++];
}
Or, if you like, you can just put the index directly on the array object, since JavaScript array objects can have arbitrary non-element properties and that helps keep the symbols together:
var fondetcaption = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
fondetcaption.index = 0;
// Call this when you click your button or whatever
function getNextBackground() {
if (fondetcaption.index >= fondetcaption.length) {
fondetcaption.index = 0;
}
return fondetcaption[fondetcaption.index++];
}
In fact, you can even make the function part of the array:
var fondetcaption = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
fondetcaption.index = 0;
fondetcaption.getNext = function() {
if (this.index >= this.length) {
this.index = 0;
}
return this[this.index++];
};
// Use
background = fondetcaption.getNext();
If making the array itself the container of those extra properties bothers you (it bothers some people), wrap the whole thing up in an object:
var fondetcaption = (function() {
var index = 0,
values = [
["fond/fond1.jpg","« aaa"],
["fond/fond2.jpg","« bbb"],
["fond/fond3.jpg","« ccc"],
["fond/fond4.jpg","« ddd"]
];
function fondetcaption_getNext() {
if (index >= values.length) {
index = 0;
}
return values[index++];
}
return {
values: values,
getNext: fondetcaption_getNext
};
})();
// Sample use:
background = fondetcaption.getNext();
// Original array still accessible if desired as fondetcaption.values