The for loop in js is executed many times, and there is a problem with the loop before exiting.

function aa () {
    (async () => {
      for (let i = 0; i < 10; iPP) {
        console.log(i, "iiiii")
        await new Promise((resolve1, reject1) => {
          (async () => {
            for (let j = 0; j < 20; jPP) {
              await new Promise((resolve, reject) => {
                setTimeout(() => {
                  resolve()
                }, 5000)
              })
              console.log(i, j)
            }
            resolve1()
          })(this)
        })
      }
    })(this)
}

execute aa () for five seconds before executing aa ()

the above two loops are executed when clicked. If I click 5 times, then for will execute the previous 4 times if it is not finished, in addition to executing the last click loop. Could you tell me how to skip the previous loop and directly execute it for the last time?

Mar.11,2022

var tmp = []
function aa (num) {
    (async () => {
      for (let i = 0; i < 10; iPP) {
        if (exist(num)) {
          break
        }
        console.log(num, 'ourter')
        await new Promise((resolve1, reject1) => {
          (async () => {
            for (let j = 0; j < 20; jPP) {
              if (exist(num)) {
                  break
              }
              console.log(num, 'inner')
              await new Promise((resolve, reject) => {
                setTimeout(() => {
                  if (!exist(num)) {
                    resolve()
                  }
                }, 5000)
              })
              console.log(i, j)
            }
            resolve1()
          })(this)
        })
      }
    })(this)
}
function exist (num) {
  if (num != tmp[tmp.length - 1]){
    return true
  }
  return false
}
tmp.push((tmp.length))
aa(tmp[tmp.length - 1])
Menu