Javascript逻辑问题
我有一个全局变量“isOnSecond”,它最初设置为false 它连接到检测布尔值的if语句,如果为true,则执行一些操作,然后将布尔值设置回false。因此,我希望这个动作有一半的时间被执行 我的问题是,我知道我在这个函数中有逻辑问题,但是我现在似乎无法想出一个解决方案 我应该如何使用所需的逻辑重新处理此函数Javascript逻辑问题,javascript,Javascript,我有一个全局变量“isOnSecond”,它最初设置为false 它连接到检测布尔值的if语句,如果为true,则执行一些操作,然后将布尔值设置回false。因此,我希望这个动作有一半的时间被执行 我的问题是,我知道我在这个函数中有逻辑问题,但是我现在似乎无法想出一个解决方案 我应该如何使用所需的逻辑重新处理此函数 function transComplete() { slideTransStep = 0; crtSlideIndex = nextSlideIndex;
function transComplete()
{
slideTransStep = 0;
crtSlideIndex = nextSlideIndex;
alert(isOnSecond);
// for IE filters, removing filters re-enables cleartype
if (nextSlide.style.removeAttribute)
nextSlide.style.removeAttribute("filter");
// show next slide
showSlide((crtSlideIndex >= totalSlides) ? 1 : crtSlideIndex + 1);
if (isOnSecond == true){
//unhighlight all controls
for (var i=0; i < slidesControllersCollection.length; i++){
if (slidesControllersCollection[i].className === slideHighlightClass)
slidesControllersCollection[i].className = ""; }
// highlight the control for the next slide
if (slidesControllersCollection[i].className === slideHighlightClass)
document.getElementById("slide-control-" + crtSlideIndex+1).className = slideHighlightClass;
isOnSecond = false;
}
isOnSecond = true;
}
函数transComplete()
{
slideTransStep=0;
crtSlideIndex=nextSlideIndex;
警报(等秒);
//对于IE筛选器,删除筛选器将重新启用cleartype
if(nextSlide.style.removeAttribute)
nextSlide.style.removeAttribute(“过滤器”);
//放映下一张幻灯片
放映幻灯片((crtSlideIndex>=总幻灯片)?1:crtSlideIndex+1);
如果(等秒==真){
//取消高亮显示所有控件
对于(变量i=0;i
结尾需要一个“else”,否则isOnSecond将始终设置为true
if (isOnSecond == true){
//unhighlight all controls
//lots of code here
isOnSecond = false;
} else {
isOnSecond = true;
}
这将始终将isOnSecond
设置为true,即使您之前刚刚将其设置为false。相反,使用
if (isOnSecond) {
// do work only every second time
isOnSecond = false;
} else {
isOnSecond = true;
}
或
此代码将确保isOnSecond
仅在一段时间内为假,只要解释器完成if语句并移到下一行,我可以向您保证这是一段非常短的时间
似乎您应该在函数末尾松开
isOnSecond=true
,并且只有在实际需要它为true时才再次声明它为true,而不是始终如此。看起来您已经完成了if语句
if (isOnSecond == true){
stuff . . .
isOnSecond = false;
}
然后将isOnSecond设置为true
isOnSecond = true;
如果您这样做:
if (isOnSecond == true){
stuff . . .
isOnSecond = false;
}else{
isOnSecond = true;
}
这将防止等秒始终显示为“真”。换句话说,如果设置为true,它将变为false,如果设置为false,它将变为true。首先,我建议将所有if语句包装在
{}
中。但是你总是在最后将isOnSecond
设置为true
,所以这可能就是问题所在谢谢大家,开始工作了,学童错误:)你需要正确地缩进代码。问题应该是显而易见的。在分配第二个值之前,选择或退出函数。
if (isOnSecond == true){
stuff . . .
isOnSecond = false;
}
isOnSecond = true;
if (isOnSecond == true){
stuff . . .
isOnSecond = false;
}else{
isOnSecond = true;
}