数组内对象中的整数不减少

Integer in object inside array not decreasing

我编写了这段代码,当 运行 将一个值添加到 mzone 到最大值 3 时,五秒后应减少添加的值,但最后一部分不起作用。我还在学习中,所以我不知道哪里出了问题。

    clubsList[playerCountry].mzone = (clubsList[playerCountry].mzone + 1) || 1; //This works perfectly. It adds 1 to the mzone everytime it's run.
    console.log(clubsList[playerCountry].mzone)
    setTimeout(function(){ (clubsList[playerCountry].mzone - 1 ) }, 5000); //This isn't working
    setTimeout(function(){ (console.log(clubsList[playerCountry].mzone)) }, 6000);

完成代码:

clubsList = []

onNet('verifyMeetingZone', (playerCountry) => {
    if (!clubsList[playerCountry]) {
        clubsList[playerCountry] = {}
        clubsList[playerCountry].mzone = 0
    }
    // HERE
    if (clubsList[playerCountry].mzone <= 2) {
        console.log("Mzone Criado")  
        clubsList[playerCountry].mzone = (clubsList[playerCountry].mzone + 1) || 1; 
        console.log(clubsList[playerCountry].mzone)
        setTimeout(function(){ (clubsList[playerCountry].mzone - 1 ) }, 5000);
        setTimeout(function(){ (console.log(clubsList[playerCountry].mzone)) }, 6000);
    }
    else {
        console.log("Ja existe")  
    }
})

我应该改变这个:

    setTimeout(function(){ (clubsList[playerCountry].mzone - 1 ) }, 5000);

对此:

    setTimeout(function(){ (clubsList[playerCountry].mzone = clubsList[playerCountry].mzone - 1) }, 5000);

我认为您没有将更改后的值重新赋值给对象,这就是对象没有获取减量的原因:

 setTimeout(function(){ clubsList[playerCountry].mzone = clubsList[playerCountry].mzone - 1  }, 5000);

试试这个,应该可以解决问题。