如何以块的形式迭代数组

时间:2015-03-16 20:00:12

标签: javascript arrays

我正在尝试构建一个从网站上获取文本的应用程序(对于我正在使用gutenburg.org的开放电子书目录的应用程序),并在div中一次显示10个索引中的故事对于那些有ADD的人来说,这个故事更容易阅读。我有一个工作增量函数,但我仍然坚持如何增加回到前一个块。

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( $( this ).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 you use crossOrigin, the dataType will be override with "json"
              //charset: 'ISO-8859-1', //use it to define the charset of the target url
              context: {},
              success: function(data) {
                  //alert(data);
                  var body = data; 
                  console.log(body.length);
                  //body/data is a 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);
        });
    });
}); 

1 个答案:

答案 0 :(得分:1)

传递属性,值可以是INCREMENT或DECREMENT

if (value=='increment')    
     array.chunk += array.chunkSize; 
else
     array.chunk -= array.chunkSize; 
相关问题