简体   繁体   English

一旦在 JavaScript 中满足条件,我将如何使事件处理程序函数中的被调用函数无法运行?

[英]How would I void a called function within an event Handler function from running once a condition is met in JavaScript?

I am working on an assignment for school and have it working.我正在为学校做作业并让它发挥作用。 The Goal was to render Pokémon to the page and have the ball shake as if something was trying to pop out of it.目标是将神奇宝贝渲染到页面并让球摇晃,好像有什么东西试图从里面弹出一样。 I have the images shaking but am not sure how to make it STOP once the final image is reached with what I have so far:我的图像在晃动,但我不确定如何在达到最终图像后停止它:

 class Eevee extends Pokemon { constructor (evolutionPath, evolutionStage) { super (evolutionPath = [ "images/pokeball.png", "images/eevee/eevee0.png", "images/eevee/eevee1.png", "images/eevee/eevee2.png", "images/eevee/eevee3.png", "images/eevee/eevee4.png", "images/eevee/eevee5.png", "images/eevee/eevee6.png", "images/eevee/eevee7.png", "images/eevee/eevee8.png" ], evolutionStage) this.evolutionStage = 0 } evolve = function () { main.append(this.image) this.image.setAttribute(`src`, `${this.evolutionPath[0]}`) this.image.addEventListener("click", function () { this.toggleShake(this.image) let evolveChance = Math.floor(Math.random( ) * 5 + 1) if (evolveChance === 1){ if (this.evolutionStage === 0){ this.evolutionStage = 1 this.image.setAttribute(`src`, `${this.evolutionPath[this.evolutionStage]}`) this.evolutionStage = Math.floor(Math.random() * ((this.evolutionPath.length)- 2) + 1) return this.image } else if (this.evolutionStage <= this.evolutionPath.length - 1 && this.evolutionStage >= 2){ this.image.setAttribute(`src`, `${this.evolutionPath[this.evolutionStage]}`) return this.image } }}.bind(this)) }.bind(this) } let eeveeA = new Eevee () eeveeA.evolve()

I was able to refactor my previous if statement and get it to work:我能够重构我以前的 if 语句并让它工作:

evolve = function () { main.append(this.image) this.image.setAttribute( src , ${this.evolutionPath[this.evolutionStage]} ) this.image.addEventListener("click", function () {进化=函数(){ main.append(this.image)this.image.setAttribute( src${this.evolutionPath[this.evolutionStage]} )this.image.addEventListener(“点击”,函数(){

let evolveChance = Math.floor(Math.random( ) * 5 + 1)

***if(this.evolutionStage === 0 || this.evolutionStage === 1) {
    this.toggleShake(this.image)
  }***
  
if (evolveChance === 1){
      if (this.evolutionStage === 0){
           this.evolutionStage = 1
           this.image.setAttribute(`src`, `${this.evolutionPath[this.evolutionStage]}`)
           this.evolutionStage = Math.floor(Math.random() * ((this.evolutionPath.length)- 1) + 2)
           return this.image
          
          } else if (this.evolutionStage <= this.evolutionPath.length - 1 && this.evolutionStage >= 2){
            this.image.setAttribute(`src`, `${this.evolutionPath[this.evolutionStage]}`)
            return this.image

          }
}}.bind(this))

}.bind(this) } }.bind(这个) }

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

相关问题 一旦满足条件,如何防止 IF 语句运行 - How can I prevent IF statement from running once the condition is met 如何在事件处理程序中包含一个函数(及其参数),而无需在页面加载时调用该函数? - How can I include a function (and its parameter) within an event handler, without the function being called on page load? 当满足某些条件时,如何通过一堆等待退出 javascript function - How can I quit from a javascript function with a bunch of awaits when certain condition is met 如果满足条件如何终止javascript函数 - How to terminate javascript function if condition is met 满足特定条件后如何禁用单击事件? - How do I disable the click event once a certain condition is met? Javascript 在事件处理程序中定义一个函数 - Javascript define a function within event handler 从事件处理程序回调中调用的函数中this的值吗? - Value of `this` in a function called from an event handler callback? 在js脚本中调用函数后,事件处理程序丢失 - event handler is missing once a function is called in js script 在Javascript中,如何从该函数中调用但在其他位置定义的函数中引用函数范围的变量? - In Javascript, how can I reference a function-scoped variable from a function called within that function but defined elsewhere? 一旦满足条件,异步调用函数? - Async call a function once condition is met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM