简体   繁体   English

如何使用另一个函数来停止 foreach 循环

[英]How to use another function to stop a foreach loop

I am using the foreach loop to call a php page that sends back html of one gallery image.我正在使用 foreach 循环调用一个 php 页面,该页面发送回一个画廊图像的 html。 The forEach Loop is triggered by the function galleryplayer. forEach 循环由函数galleryplayer 触发。 Gallery size can be anything from 1 to 99 photos max.图库大小可以是 1 到 99 张照片。

I want to use the function stopGalPlay to halt the foreach loop on the iteration it is currently on.我想使用函数 stopGalPlay 在当前正在进行的迭代中停止 foreach 循环。 However after a week of trying to solve this I cannot think how.但是,经过一周的尝试解决此问题后,我想不出如何解决。

Reading about it, the consensus says that I should not be using a forEach loop.阅读它,共识说我不应该使用 forEach 循环。 If so how would I rewrite the galleryPlayer function to use a different kind of loop and/or incorporate some mechanism to break the loop and execute the code in the stopGalPlay function.如果是这样,我将如何重写 galleryPlayer 函数以使用不同类型的循环和/或合并一些机制来打破循环并执行 stopGalPlay 函数中的代码。

I know the question of breaking a foreach loop has been answered a million times before but I cannot figure out how to incorporate a stop play into my image gallery.我知道打破 foreach 循环的问题之前已经回答了一百万次,但我不知道如何将停止播放合并到我的图片库中。

Any help would be greatly appreciated.任何帮助将不胜感激。 Note that I want to use pure javascript without libraries like jquery or others.请注意,我想使用纯 javascript,而没有像 jquery 或其他库这样的库。

var detailPageTabContentContainer = document.getElementById("detailPageTabContentContainer");

// Open the Modal
function openGalleryModal(galId, count, rowTotal) {
  var galId = arguments[0];
  var count = arguments[1];
  var rowTotal = arguments[2];
  const api_url = "includes/ajax/GalleryViewerXhr.php?galId=" + galId + "&&count=" + count + "&&rowTotal=" + rowTotal;
    fetch(api_url, { method: 'get', credentials: 'same-origin' })
      .then(response => response.text())
      .then(html => {
    detailPageTabContentContainer.innerHTML = html;
  })
      .catch((err) => console.log("Can’t access " + api_url + err));
} // End of function openGalleryModal


// Close the Modal
function closeGalleryModal() {
  document.getElementById("galleryModal").style.display = "none";
  document.getElementById("active").click();
} // End of function closeGalleryModal


function plusGallerySlides(galId, count, rowTotal, n) {
  var galId = parseInt(arguments[0]);
  var count = parseInt(arguments[1]);
  var rowTotal = parseInt(arguments[2]);
  var n = (arguments[3]);
  var GalIdFragment = (Math.floor(galId / 100));
  var GalIdFragmentString = GalIdFragment.toString();
  var countString;
  if (count + n > rowTotal) {newCount = 1}
  if (count + n < 1) {newCount = rowTotal}
  if (count + n == rowTotal) {newCount = rowTotal}
  if ((count + n < rowTotal)&&(count + n != 0)) {newCount = count + n}
  if (count.toString().length == 1) {countString = "0" + count}
  else {countString = count}
  if (newCount.toString().length == 1) {countString = "0" + newCount } else {countString = newCount};
  countString = countString.toString();
  newGalId = GalIdFragmentString + countString;
  const api_url = "includes/ajax/GalleryViewerXhr.php?galId=" + newGalId + "&&count=" + newCount + "&&rowTotal=" + rowTotal;
    fetch(api_url, { method: 'get', credentials: 'same-origin' })
      .then(response => response.text())
      .then(html => {
    detailPageTabContentContainer.innerHTML = html;
  })
      .catch((err) => console.log("Can’t access " + api_url + err));
} // End of function plusGallerySlides


function stopGalPlay(galId, count, rowTotal) {
  var galId = parseInt(arguments[0]);
  var count = parseInt(arguments[1]);
  var rowTotal = parseInt(arguments[2]);
  console.log("gallery Id is " + galId + ". Count is " + count + ". Row total is " + rowTotal + ".");
  const api_url = "includes/ajax/GalleryViewerXhr.php?galId=" + galId + "&&count=" + count + "&&rowTotal=" + rowTotal;
    fetch(api_url, { method: 'get', credentials: 'same-origin' })
      .then(response => response.text())
      .then(html => {
    detailPageTabContentContainer.innerHTML = html;
  })
      .catch((err) => console.log("Can’t access " + api_url + err));
}


function galleryplayer(galId, count, rowTotal) {
  var galId = parseInt(arguments[0]);
  var count = parseInt(arguments[1]);
  var rowTotal = parseInt(arguments[2]);
  var GalIdFragment = (Math.floor(galId / 100));
  var GalIdFragmentString = GalIdFragment.toString();
  var galIdArr = [];
  for ( i = 1; i <= rowTotal ; i++ ) {
   galIdArr.push(i < 10 ? (GalIdFragmentString + "0" + i.toString()) : GalIdFragmentString + i.toString())
  };
  var interval = 4950;
  var promise = Promise.resolve();
    galIdArr.forEach(function (ArrayGalId, i) {
    promise = promise.then(function() {
      const api_url = "includes/ajax/GalleryViewerXhr.php?galId=" + ArrayGalId + "&&count=" + (i+1) + "&&rowTotal=" + rowTotal;
        fetch(api_url, { method: 'get', credentials: 'same-origin' })
          .then(response => response.text())
          .then(html => {
        detailPageTabContentContainer.innerHTML = html;
        document.getElementById("galNext").style.display = "none";
        document.getElementById("galPrev").style.display = "none";
        document.getElementById("galPlay").style.display = "none";
        document.getElementById("galClose").style.display = "none";
        document.getElementById("galPhoto").classList.add("Gallery-Player-Fade-in-Out");
      })
      return new Promise(function(resolve) {
        setTimeout(resolve, interval);
        if((i+1) === (galIdArr.length)) {
          var lastGalId = (galIdArr[galIdArr.length -1]);
          var galLength = galIdArr.length;
          const api_url = "includes/ajax/GalleryViewerXhr.php?galId=" + lastGalId + "&&count=" + galLength + "&&rowTotal=" + rowTotal;
            fetch(api_url, { method: 'get', credentials: 'same-origin' })
            .then(response => response.text())
            .then(html => {
          detailPageTabContentContainer.innerHTML = html;
          });
        };
      });
    });
  }); 
}// End of function galleryplayer

You could use a while loop.您可以使用 while 循环。 while this condition is false do this.虽然此条件为假,但请执行此操作。 Or you could put the forEach loop inside of a function and return when it is supposed to break.或者您可以将 forEach 循环放在函数内部,并在它应该中断时返回。

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

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