javascript - How to iterate down an Array in chunks -
i'm trying build application takes text website (for app i'm using gutenburg.org's open ebook catalog) , displays story in bites of 10 indexes @ time in div make story easier read add. have working increment function i'm stuck how increment previous chunk.
html: <input type="text" id="url" style="width: 400px"> <input type="button" id="btn" value="get json"> <button id="button">next</button> <button id="prev">previous</button> <div id="test"></div> javscript: $(function() { $( '#service' ).on( 'change', function(){ $( '#url' ).val( $( ).val() ); }); //angular.module('exampleapp') $( '#url' ).val( $( '#service' ).val() ); $( '#btn' ).click(function(){ var url = $( '#url' ).val() $.ajax({ crossorigin: true, proxy: "http://localhost:8888/whorl/proxy.php", url: url, //datatype: "json", //no need. if use crossorigin, datatype override "json" //charset: 'iso-8859-1', //use define charset of target url context: {}, success: function(data) { //alert(data); var body = data; console.log(body.length); //body/data string var text = body.split(' ') console.log(text.length); var increment = function(array) { if (array.chunk < array.length) { var chunk = array.slice(array.chunk,math.min(array.chunk+array.chunksize, array.length).join(" "); array.chunk += array.chunksize; $( '#test' ).html(chunk); console.log(chunk); } }; $(document).ready(function(){ $("button").click(function() { increment(text); }); }); } }) .done(function( data, textstatus, jqxhr ) { //alert(data); }); }); });
pass attribute, value either increment or decrement
if (value=='increment') array.chunk += array.chunksize; else array.chunk -= array.chunksize;
Comments
Post a Comment