【问题标题】:I am trying to have the achievement stop when it shows, but it will either not show at all or keep showing. I want it to show only once我试图让成就在显示时停止,但它要么根本不显示,要么继续显示。我希望它只显示一次
【发布时间】:2022-11-26 02:08:46
【问题描述】:

我会尝试早点解决它,但我不知道怎么说。

我尝试添加另一个参数,但效果不佳。我做了一个变量来检测成就是否已经显示。我想我把它放在了错误的位置,或者我根本不需要它:

var achived = false; 

 function advance(points, a, sub, ec){
    ect = '+' + ec;
   if(score == points && !achived){
    achived = true;
    Swal.fire({
title: "<h4 style= 'color: white'> Achievement Unlocked: </h4>",
background: "black",
text: a,
footer: sub + ' ' + ect,confirmButtonColor: '#131313',
})

score = score + ec;$('#score').text(score); 

achived = false;
}

... 

function gameOver(){

        var aud = $('audio')[1]
        aud.play();

        score = score + 1;
        score = score + n;
        console.log(`+1`);
        $('#score').text(score);
        changePosition();
        
       advance(5, "Getting Started", "Get 5 points", 1);
       advance(10, "No Longer a Rookie", "Get 10 points", 1);
       advance(15, "More Than a Beginner", "Get 15 points", 1);
       advance(20, "Getting The Hang of It!", "Get 20 points", 1);
       advance(30, "Beating the Average", "Get 30 points", 2);
       advance(45, "More Than a Master", "Get 45 points", 2);
       advance(50, "OG", "Get 50 points", 1);
       advance(60, "Hacker", "Get 60 points", 2);
       advance(90, "King of Twurkeys", "Get 90 points", 3);
       advance(100, "Pro Hacker", "Get 100 points", 2);






  
    }

【问题讨论】:

  • 像那样的东西。我希望它只显示一次,而不是只运行一个函数。 (如果你不知道的话,我说的是 achived 变量)
  • 请阐明您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • 我试图在每个函数中只显示一次成就。我不希望函数重复已经运行过的函数。

标签: javascript jquery sweetalert


【解决方案1】:

我不确定我是否理解正确,但我认为你只想基于布尔标志运行一段代码?

如果是这样,那么如果您的标志为真,则您需要阻止代码的执行(例如提前返回)。

这是一个简单的例子,说明它是如何工作的:

let hasFired = false;

const onFire = () => {
  if (hasFired) {
    return; // don't run the code below
  }

  console.log('Fired!');
  
  hasFired = true;
}

onFire();
onFire();
onFire();

// 'Fired!' visible only once

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2012-11-18
    • 2021-12-26
    相关资源
    最近更新 更多