I'm trying to write a function that takes a large array and iterates up and down it in a set number of chunks via a previous and next button. I have the next button working fine but cannot get it to reverse the array the same way I go forward. Here's what I have:
javscript:
success: function(data) {
//alert(data);
var body = data;
console.log(body.length);
//body/data is a string
var text = body.split(' ');
text.chunk = 0; text.chunkSize = 15;
// var text = body.split(' ');
var next = true;
console.log(text.length);
var increment = function(array,next) {
if (array.chunk < array.length) {
var slice = array.slice(array.chunk,Math.min(array.chunk+array.chunkSize, array.length));
var chunk = slice.join(" ");
if (next) {
array.chunk += array.chunkSize;
$( '#test' ).html('<p>' + chunk + '</p>');
console.log(chunk);
}else {
var slice = array.slice(array.chunk,Math.min(array.chunk+array.chunkSize, array.length));
array.chunk -= array.chunkSize;
//chunk = array.chunk - array.chunkSize;
$( '#test' ).html(chunk);
console.log(chunk);
}
}
}
$("#prev").click(function() {
increment(text);
});
$("#button").click(function() {
increment(text, next);
});
Aucun commentaire:
Enregistrer un commentaire