A piece of code processes the input line taken from the base, if the line length is more than 1000 characters, it is paginated and poured into the block with the addition of page switches On the first pass, that is, immediately after the page loads (and the first access to the database) everything goes as need to. But if the user initiates an AJAX call to replace the text (not the page, but the entire text for the block), the browser loops on the third to sixth line

if(ii>item.page_text.length){ ii=item.page_text.length; ii--; } 

Question: why?

Important clarification The code does not just divide the line into fragments of 1000 characters. He is looking for the nearest end of the sentence indicated by a dot in the direction of decreasing the number of characters.

Here is the responsible piece of code

  var i=0; var ii=0; pageText=new Array(); for( ii=1000;i<item.page_text.length-1;ii=i+1000) { if(ii<item.page_text.length){ while(ii>i){ if (item.page_text[ii]!="."){ ii--; } else { ii++; pageText.push("<p>"+item.page_text.slice(i,ii)+"</p>"); i=ii; $(".book-text-choose-first").before("<span class=\"book-text-page-num\" id=\"page#"+pageText.length+"\">"+pageText.length+"</span>"); break; } } } else{ ii=item.page_text.length-1; } } 
  • What is the value of i at the very beginning of the given fragment? - Sergiks
  • and I apologize, I lost the string var i=0 - Konorlevich
  • See the execution of this code step by step in the debugger. In Chrome developer tools or FireBug. - Sergiks
  • I looked. That is how I understood on which lines the browser hangs. But still do not understand why. - Konorlevich
  • one
    Yes, I found. I have a mistake in logic. the conditional operator checking for exceeding the permissible value did not direct further code execution, but to the completion of the iteration of the for loop, which at the beginning of the new iteration added 1000 to the desired value, which again directed the if to decrease the value and so on. Thanks for the help. - Konorlevich

1 answer 1

In the code with many cycles and counters, it is very hard to find an error, this is the code, I think it will suit you better

 /*в переменной page должен быть текст статьи, переменная content должна указывать на тег в котором должна быть статья, а тег navBar на тег в котором должна быть навигация, я использовал интервал 10, в вашем случае поменяйте его на 1000, после чего можете применять этот код*/ var page="aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee"; var content=document.getElementById("content"); var navBar=document.getElementById("navbar"); var listPages=[]; content.textContent=page.substring(0,10); for(var i=1;page.length>=10;i++){ listPages[i]=page.substring(0,10); page=page.substring(10,page.length); var navIndex=document.createElement("a"); navIndex.href=""; navIndex.textContent=""+i; navIndex.addEventListener("click",function(e){ e.preventDefault(); var textNavIndex=e.currentTarget.textContent; content.textContent=listPages[parseInt(textNavIndex)]; },false); navBar.appendChild(navIndex); } 
  <div> <div id="content"></div> <div id="navbar"></div> </div> 

  • I thank for the decision, but I would like to understand exactly what I was doing wrong , what I was wrong to do in the future. If it is not very difficult, tell me how it is not faster to understand, without replacing your code thoughtlessly with yours. - Konorlevich
  • I can’t help with anything, my head is spinning from cycles) - user205867
  • Okay, anyway, thanks =) By the way, in the site notifications there is an unopened first edition of the answer, I think, yours. In combination with the current combed version, it looks absolutely incomparable =) - Konorlevich