在倒数计时器结束时设置变量为false
Set variable to false at the end of countdown timer
我可能只是在这里密集…我有一个变量timeDiscount
,当页面加载时是true
。同样,当页面加载时,计数器开始。在计数器结束时,我将timeDiscount
设置为false
…除了这似乎不工作…
在这个jsFiddle中,单击"CLICK"字将提醒您timeDiscount
的当前状态,即使在计数器停止后,单击也会返回true。为什么呢?
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
refreshIntervalId = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
clearInterval(refreshIntervalId)
timeDiscount = false;
}
}, 1000);
}
jQuery(function ($) {
var timeDiscount = true
var fiveMinutes = 5,
display = $('#time');
startTimer(fiveMinutes, display);
$("#discount").click(function() {
alert(timeDiscount);
})
});
你有一个作用域问题。
在您的代码中,timeDiscount
在页面加载时执行的未命名函数(jQuery(function ($) {...
)中声明。这个变量只有这个函数内部的标识符知道,但是你试图从startTimer
函数访问它。
将声明移出未命名函数:
var timeDiscount = true
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
refreshIntervalId = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
clearInterval(refreshIntervalId)
timeDiscount = false;
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 5,
display = $('#time');
startTimer(fiveMinutes, display);
$("#discount").click(function() {
alert(timeDiscount);
})
});
变量必须是全局变量才能在javascript函数中访问它,所以要把它放在外部级别
var timeDiscount = true
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
refreshIntervalId = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
clearInterval(refreshIntervalId)
timeDiscount = false;
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 5,
display = $('#time');
startTimer(fiveMinutes, display);
$("#discount").click(function() {
alert(timeDiscount);
})
});
首先有两种方法:将timeDiscount
变量移出函数,使其成为全局变量,如下所示:
var timeDiscount = true
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
refreshIntervalId = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
clearInterval(refreshIntervalId)
timeDiscount = false;
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 5,
display = $('#time');
startTimer(fiveMinutes, display);
$("#discount").click(function() {
alert(timeDiscount);
})
});
或者你可以简单地将window
添加到你的timeDiscount
函数中,就像这样,并使它成为window.timeDiscount
(参考这个链接在JavaScript函数中定义全局变量):
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
refreshIntervalId = setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
clearInterval(refreshIntervalId)
timeDiscount = false;
}
}, 1000);
}
jQuery(function ($) {
window.timeDiscount = true
var fiveMinutes = 5,
display = $('#time');
startTimer(fiveMinutes, display);
$("#discount").click(function() {
alert(timeDiscount);
})
});
您需要在全局范围内声明timeDiscount
变量,即在startTimer
函数之前,这样当您在startTimer()
中引用相同的变量时,它将对它可用,否则它将创建一个全局变量并将其设置为false
。
在全局作用域中创建的变量(timeDiscount
)与在局部作用域中存在的$(function() { var timeDiscount=true; })
中的变量完全不同,这就是为什么您从未得到任何错误,也没有得到您所期望的。
JS代码:
var timeDiscount = true;
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
timer = duration;
timeDiscount = false;
}
}, 1000);
}
Live Demo @ jsfield
- 当对象中的所有变量都为false时,AngularJs禁用按钮
- 如果循环中至少有一个元素返回 false,如何将变量设置为 false
- 如何将javascript变量传递给php文件,运行数据库查询并返回true或false
- 当值与变量关联时,JavaScript 条件为 false
- 为什么Javascript中未定义的变量有时会计算为false,有时还会抛出未捕获的ReferenceError
- 为什么JavaScript在比较一个变量和两个true语句时返回false
- 如何将对正在定义或未定义的变量的检查强制转换为读取True或False的字符串
- 变量=true,即使if语句为false
- 基于true/false变量显示/隐藏进度条
- 检查变量是否为false,而不是true或未定义
- link_to_remote:如果javascript变量为false,我该如何中止操作
- Meteor:设置车把变量true/false
- 轻松地将多个变量设置为false或true
- 未定义的变量,值==false对!价值
- jQuery Ajax更改缓存变量名称:false
- 在简写if表达式中,值变量为0而不是false
- 变量的计算结果总是false
- 如何轻松地将变量设置为false,而默认值为true ?
- $. inarray()返回false -1,即使变量匹配数组
- 在倒数计时器结束时设置变量为false