简体   繁体   English

如何打破javascript中的循环?

[英]How to break loop in javascript?

const arrow = document.querySelector("#arrow");
const callAllPie = document.querySelector(".allPie");
eventList();
function eventList(e) {
  arrow.addEventListener("click", showSkills);
}
function showSkills() {
  let element = callAllPie;
  for (let i = 0, n = 4; i < n; i++) {
    const pie = document.createElement("div");
    pie.classList.add("pie1");
    callAllPie.appendChild(pie);
    const rightDiv = document.createElement("div");
    rightDiv.classList.add("slice-right1");
    const leftDiv = document.createElement("div");
    leftDiv.classList.add("slice-left1");
    const percentDiv = document.createElement("div");
    percentDiv.classList.add("percent1");
    const numberDiv = document.createElement("div");
    numberDiv.classList.add("number1");
    numberDiv.innerHTML = "%99";
    const nameDiv = document.createElement("div");
    nameDiv.classList.add("name1");
    nameDiv.innerHTML = "HTML";
    pie.appendChild(rightDiv);
    pie.appendChild(leftDiv);
    pie.appendChild(percentDiv);
    percentDiv.appendChild(numberDiv);
    percentDiv.appendChild(nameDiv);
    callAllPie.appendChild(pie);
  }
}

I want to break the loop after the click event runs once, how do I do it?我想在点击事件运行一次后打破循环,我该怎么做? When run the click event, added pie div to my page but when I clicked again, it being created again.运行点击事件时,将 pie div 添加到我的页面,但是当我再次点击时,它又被创建了。

You could simply remove the click listener at the beginning of your showSkills() function so no further clicks trigger an action:您可以简单地删除showSkills()函数开头的点击侦听器,这样就不会再有点击触发操作:

function showSkills() {
    arrow.removeEventListener('click', showSkills);
    let element = callAllPie;
    ....
}

Or as Teemu points out, a much cleaner approach is to set once: true in the options parameter:或者正如 Teemu 指出的那样,一种更简洁的方法是在 options 参数中设置once: true

arrow.addEventListener("click", showSkills, {once: true});

You can make a boolean isLoopRunning and use it to see if the loop Is already running您可以制作一个布尔值isLoopRunning并使用它来查看循环是否已经在运行

const arrow = document.querySelector("#arrow");
const callAllPie = document.querySelector(".allPie");
eventList();
let isLoopRunning = false;
function eventList(e) {
  arrow.addEventListener("click", showSkills);
}
function showSkills() {
  let element = callAllPie;
  isLoopRunning = !isLoopRunning;
  for (let i = 0, n = 4; i < n; i++) {
    if (isLoopRunning) {
      // your code here
    } else {
      break;
    }
  }
}

The break statement terminates the current loop. break 语句终止当前循环。

if condition satisfies requirement just use break keyword inside loop如果条件满足要求,只需在循环内使用 break 关键字

for(let i=0; i<10; i++){
if(i==6){
console.log(i)
break;
}
console.log("hi")
}

"hi" will not print after 6 times “hi”在 6 次后不会打印

When using a status variable outside the function you will have the option to reset the status elsewhere in the code and have the loop run once again.在函数外部使用状态变量时,您可以选择在代码的其他地方重置状态并再次运行循环。


var showSkills = true;

// Runs once upon user click
function showSkills() {
   if (showSkills) {

      for {
          /* Do the loop */
      };

      showSkills = false;
   };
}

// If you need to run it again upon user click.
// E.g. attach it to some 'reset' button.
function reset() {
   showSkills = true;
}

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

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