如果变量为真,不显示警报?Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15693944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if variable is true, DON'T SHOW alert? Javascript
提问by Articulous
So basically, what I'm trying to find out, is if there is any way to set a variable to false, and NOT SHOW an alert message if said variable is changed to true.
所以基本上,我试图找出的是,是否有任何方法可以将变量设置为 false,并且如果所述变量更改为 true,则不显示警报消息。
example:
例子:
function pausePop() {
$("#signatures").hover(function() {
var shown = false;
alert('If you hover over the signatures or the cleared sides, it will pause the scroll');
var shown = true;
if (shown = true) {
}
});
}
Is there any way to do this? If so, is there a SIMPLE way to do this? I am a mere beginner and would like to do some cool things right off the bat.
有没有办法做到这一点?如果是这样,是否有一种简单的方法可以做到这一点?我只是一个初学者,想立即做一些很酷的事情。
采纳答案by chrx
I'm not sure of the context, but if you'd like to trigger the alert only once and then mark it as shown, try:
我不确定上下文,但如果您只想触发警报一次,然后将其标记为所示,请尝试:
var shown = false;
function pausePop() {
if (!shown) {
alert('If you hover over the signatures or the cleared sides, it will pause the scroll');
shown = true;
}
}
$("#signatures").hover(pausePop);
回答by jahroy
Here's a jsFiddle that shows you some vary basic concepts related to your question:
这是一个 jsFiddle,它向您展示了与您的问题相关的一些不同的基本概念:
Feel free to modify it however you like.
随意修改它。
Here's a preview of the code:
这是代码的预览:
var showAlert = true;
$('#hoverDiv').hover(function () {
if (showAlert) {
alert("Make sure you read this... You will only see it once.");
}
showAlert = false;
});
回答by yet
I think you are starting Javascript as your first programming language or you are just playing with some code.
我认为您开始将 Javascript 作为您的第一门编程语言,或者您只是在玩一些代码。
The problem is not about Javascript, its about conditional if/else statements and flow of execution.
问题不在于 Javascript,而在于条件 if/else 语句和执行流程。
you dont need if/else also, if alert is shown execution pauses on that line. after client clicks the ok button, next statement will be evaluated.
您也不需要 if/else,如果显示警报,则该行会暂停执行。客户端单击确定按钮后,将评估下一条语句。