简体   繁体   English

如何清除if语句中的间隔

[英]How to clear an interval in an if statement

So, I'm working on a game called Mini Arcade which features a bunch of different minigames.所以,我正在开发一款名为 Mini Arcade 的游戏,其中包含许多不同的小游戏。 I found a way to create a fade effect after I click a button to go to another page.我找到了一种在单击按钮转到另一个页面后创建淡入淡出效果的方法。 The fade effect seems to work properly, but after going to the console of the page, the fade interval doesn't clear like it's supposed to when the opacity variable hits 0, which makes the opacity of the object go into the negatives.淡入淡出效果似乎工作正常,但是在转到页面的控制台后,淡入淡出间隔不像在不透明度变量达到 0 时应该清除的那样清晰,这使得对象的不透明度变为负值。 This is a problem because once I begin making buttons that fade back to the homepage, the homepage will still be fading out, while fading in at the same time, which is a big problem.这是一个问题,因为一旦我开始制作淡回主页的按钮,主页仍然会淡出,同时淡入,这是一个大问题。

if(opacity == 0){
clearInterval(fadeOut);
}

As you can tell, it's just a simple if statement within my fade out function.如您所知,这只是我的淡出函数中的一个简单 if 语句。 Sorry if I didn't explain this well, but I could really use some help.对不起,如果我没有很好地解释这一点,但我真的可以使用一些帮助。 Thanks.谢谢。

You need to initiate your Interval first and assign it to a variable:您需要先启动您的 Interval 并将其分配给一个变量:

var myInterval = window.setInterval(fadeOut, 1000);

then you can clear it with clearInterval:然后你可以用 clearInterval 清除它:

clearInterval(myInterval);

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

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