javascript - Maintain five section in DOM at a time -
i creating ebook reader, want fetch sections of ebook link :
http://tmaserv.scem.uws.edu.au/chapters/?n=0 fetch section number 0 http://tmaserv.scem.uws.edu.au/chapters/?n=1 fetch section number 1 .. on
number of sections can obtained url http://tmaserv.scem.uws.edu.au/chapters
now, able fetch section, want reader should maintain 5 sections of document in dom @ time.also can fetch 1 section @ time. first 5 fetched document loads,then additional sections fetched on request.
code:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>ebook</title> <script> var requestnum = 0; // assume had fetched number of sections in varibale sectioncount function do_exercise () { var x = new xmlhttprequest(); // adjust url reflect new n value , request before x.open('get', 'http://tmaserv.scem.uws.edu.au/chapters/?n=' + requestnum, true); x.onreadystatechange = function() { if (x.readystate == 4 && x.status ==200) { obj = (x.responsetext); json.parse(x.responsetext); obj = json.parse(obj); document.getelementbyid("section1").innerhtml = obj.data; requestnum++; } } x.send(null); } </script> <body> <nav> <button onclick="do_exercise();">next section</button> </nav> <section id = "section1"> </section> </body> </html>
should use array 5 sections , maintain them?
are trying this?
var cachesize = 5; var chapters = []; var requestnum = 0; function fetchchapter() { var x = new xmlhttprequest(); // adjust url reflect new n value , request before x.open('get', 'http://tmaserv.scem.uws.edu.au/chapters/?n=' + requestnum++, true); x.onreadystatechange = function() { if (x.readystate == 4 && x.status == 200) { obj = (x.responsetext); json.parse(x.responsetext); obj = json.parse(obj); chapters.push(obj.data); if (chapters.length > cachesize) { chapters.shift(); } (var = 0; < chapters.length; ++i) { document.getelementbyid("section" + i).innerhtml = chapters[i]; } } } x.send(null); }
Comments
Post a Comment