简体   繁体   English

数组中特定位置的html页面

[英]html page at a specific place in an array

I am working on this project that I have with multiple HTML pages that are kept in an array. 我正在处理这个项目,我将多个HTML页面保存在一个数组中。 Each time the user finishes his task on one page then array shuffles itself and loads the remaining HTML pages in and display for the user. 每次用户在一页上完成其任务后,数组会自动洗牌并在其中加载其余HTML页面并为用户显示。

var getPages = localStorage.getItem("htmlPages");

if (document.title === "INDEX" || !getPages) {
  var htmlPages = [
    "page1.html",
    "page2.html",
    "page3.html",
    "page4.html",
    "page5.html",
    "page6.html",
    "page7.html",
    "page8.html",
    "page9.html",
    "page10.html"
  ];
  console.log('initializing html pages BEGIN');
  var jsonhtmlPages = JSON.stringify(htmlPages);
  window.localStorage.setItem("htmlPages", jsonhtmlPages);
  console.log('initializing html pages END');
} else {
  console.log('filling html pages');
  var htmlPages = JSON.parse(getPages);
}

function RandomPages() {
  shuffle(htmlPages);
  if (htmlPages.length > 0) {
    window.location.href = htmlPages[0];
  } else {
    console.log("tasks completed");
    alert('ALL PAGES ARE DONE');
  }
  console.log(htmlPages.shift());
  console.log(htmlPages);
  var jsonPool = JSON.stringify(htmlPages);
  window.localStorage.setItem("htmlPages", jsonPool);
  console.log(htmlPages);
}

This above works fine, but now I want to add a break.html page after every 3 pages that have been visited by the user. 上面的方法工作正常,但是现在我想在用户访问的每3页之后添加一个break.html页。 I used 我用了

htmlPages.splice(3, 0, "Break.html");

but this is not the right solution or if it is then where to add it? 但这不是正确的解决方案,或者如果在哪里添加呢? coz the array shuffles and the break.html wouldn't keep its place. 因为数组随机播放,所以break.html不会保留其位置。 Any help would be appreciated, thanks 任何帮助,将不胜感激,谢谢

I would increment a counter every call of RandomPages , like pageViews++ . 我将在每次调用RandomPages都增加一个计数器,例如pageViews++ This should be defined outside of the RandomPages method so it doesn't get reset every call. 这应该在RandomPages方法之外定义,这样就不会在每次调用时都将其重置。

Then before you check if (htmlPages.length > 0) I would add another check for 然后,在您检查if (htmlPages.length > 0)我将添加另一项检查

if (pageViews % 3 === 0) { 
  /* show break page and return/don't execute the next if check */ 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM