I have the following code.
我有以下代码。
var stackMapIn = [];
var stackMapOut = [];
var stackBack = [];
stackMapOut.push("m1");
$scope.clickLinks = function(initialOut,initialIn,extra,backIn,backOut,name){
$('div#'+initialOut+'Map,'+extra).fadeOut('slow',function(){
$('.'+initialOut+'Details,.bkbtn'+backOut).css("display","none");
$('.'+initialIn+'Details,.bkbtn'+backIn).css("display","block");
$('.noQuery').css("display","none");
$("#buildingHeader").html(name);
$('div#'+initialIn+'Map').fadeIn('slow');
})
stackMapOut.push(initialIn);
stackMapIn.push(initialOut);
stackBack.push(backIn);
}
$scope.clickBack = function(bkbtnCheck){
alert(stackBack[0]);
mapOut = stackMapOut.pop();
mapIn = stackMapIn.pop();
stackBack.pop();
backIn = stackBack[0];
alert(backIn);
$('div#'+mapOut+'Map').fadeOut('slow',function(){
$('.'+ mapOut + 'Details,.bkbtn').css("display", "none");
$('.' + mapIn + 'Details,.bkbtn'+backIn).css("display", "block");
$(".noQuery").css("display","none");
$("#buildingHeader").html("Name");
$('div#' + mapIn + 'Map').fadeIn('slow');
})
}
Now I am going to do a quick run-through of what happens as this code runs.
现在,我将快速浏览一下这段代码运行时会发生什么。
The first time clickLinks runs:
第一次运行clickLinks:
initialIn = 'm2'
initialIn ='m2'
initialOut = 'm1'
initialOut ='m1'
backIn = 'Home'
backIn ='主页'
clickBack has not run yet.
clickBack还没有运行。
The second time clickLinks runs:
第二次clickLinks运行:
initialIn = 'm7'
initialIn ='m7'
initialOut = 'm2'
initialOut ='m2'
backIn = 'CentralPortfolio'
backIn ='CentralPortfolio'
Ok so at this point things should be looking like this (I expect):
好的,所以在这一点上应该看起来像这样(我期待):
stackMapOut = ['m1','m2','m7']
stackMapOut = ['m1','m2','m7']
stackMapIn = ['m1','m2']
stackMapIn = ['m1','m2']
stackback = ['Home','CentralPortfolio']
stackback = ['Home','CentralPortfolio']
Now we run clickBack... Why does the alert output "Home"??
现在我们运行clickBack ...为什么警报输出“Home”?
What I am trying to do here is, I have a series of things appearing and disappearing when clickLinks runs. Sometimes, the user can run clickBack in order to return to the previous state(status). So, I am using JavaScript stack to keep track of what state it is on and thus where it needs to return.
我在这里要做的是,当clickLinks运行时,我有一系列的东西出现并消失。有时,用户可以运行clickBack以返回先前的状态(状态)。所以,我正在使用JavaScript堆栈来跟踪它所处的状态,以及它需要返回的位置。
The problem is, I can run clickLinks once, clickBack once consecutively without issue. I can even run clickLinks a second time and still click the back button 2 times (to return to start) without issue. But I still don't understand why stackBack[0] (which should be the top of the stack ?) = "Home" at this point instead of "CentralPortfolio".
问题是,我可以运行clickLinks一次,clickBack连续一次没有问题。我甚至可以再次运行clickLinks并仍然单击后退按钮2次(返回开始)没有问题。但是我仍然不明白为什么stackBack [0](应该是堆栈顶部?)=“Home”而不是“CentralPortfolio”。
Because the real problem that I run into is now if I run clickLinks a third time: still stackBack[0] = 'Home' (when I expect it to be 'CentralCampus' at this point) and thus, it is the "Home" back button that is showing (while the correct other stuff is showing in accordance to mapOut and mapIn) instead of CentralPortfolio' back button to be showing; since I have 'CentralCampus' "popped" off before I use it.
因为我遇到的真正问题是如果我第三次运行clickLinks:仍然是stackBack [0] ='Home'(当我希望它在此时为'CentralCampus')时,它就是“Home”显示的后退按钮(当正确的其他内容按照mapOut和mapIn显示时)而不是显示的CentralPortfolio'后退按钮;因为我在使用它之前“弹出”了“CentralCampus”。
Please, if any other information is needed or you need more clarification let me know. I tried my best to provide any needed information and make it as clear as possible.
如果需要任何其他信息,或者您需要更多说明,请告诉我。我尽力提供任何所需信息并尽可能清楚地表达。
1 个解决方案
#1
Now we run clickBack... Why does the alert output "Home"??`
现在我们运行clickBack ...为什么警报输出“Home”??`
because stackBack[0]
is Home
. What would you expect it to do? You even have it in your question:
因为stackBack [0]是Home。你期望它做什么?你甚至在你的问题中有它:
stackback = ['Home','CentralPortfolio']
The index 0
is the first element in your array, which is Home
. The last element is stackBack[stackBack.length - 1]
索引0是数组中的第一个元素,即Home。最后一个元素是stackBack [stackBack.length - 1]
using JavaScript stack
使用JavaScript堆栈
No, you are using a javascript array, but using it like a stack. Except when you index it like an array - which is the root of your confusion. You can use it as a stack with pop
and push
, but then don't try and index it with []
不,你使用的是javascript数组,但是像堆栈一样使用它。除非您将其索引为数组 - 这是您混淆的根源。您可以将它用作pop和push的堆栈,但不要尝试使用[]将其编入索引
When you push
on an array, you add an element to the end of the array. In other words, it ends up as the last element at the index yourArray.length - 1
. When you pop
, you take that last element off the array again. So popping stackback
would give you back CentralPortfolio
, and, of course, the first element is unchanged.
当您按下数组时,您会在数组末尾添加一个元素。换句话说,它最终成为索引的最后一个元素yourArray.length - 1.当你弹出时,你再次从数组中取出最后一个元素。所以popping stackback会让你回到CentralPortfolio,当然,第一个元素没有变化。
#1
Now we run clickBack... Why does the alert output "Home"??`
现在我们运行clickBack ...为什么警报输出“Home”??`
because stackBack[0]
is Home
. What would you expect it to do? You even have it in your question:
因为stackBack [0]是Home。你期望它做什么?你甚至在你的问题中有它:
stackback = ['Home','CentralPortfolio']
The index 0
is the first element in your array, which is Home
. The last element is stackBack[stackBack.length - 1]
索引0是数组中的第一个元素,即Home。最后一个元素是stackBack [stackBack.length - 1]
using JavaScript stack
使用JavaScript堆栈
No, you are using a javascript array, but using it like a stack. Except when you index it like an array - which is the root of your confusion. You can use it as a stack with pop
and push
, but then don't try and index it with []
不,你使用的是javascript数组,但是像堆栈一样使用它。除非您将其索引为数组 - 这是您混淆的根源。您可以将它用作pop和push的堆栈,但不要尝试使用[]将其编入索引
When you push
on an array, you add an element to the end of the array. In other words, it ends up as the last element at the index yourArray.length - 1
. When you pop
, you take that last element off the array again. So popping stackback
would give you back CentralPortfolio
, and, of course, the first element is unchanged.
当您按下数组时,您会在数组末尾添加一个元素。换句话说,它最终成为索引的最后一个元素yourArray.length - 1.当你弹出时,你再次从数组中取出最后一个元素。所以popping stackback会让你回到CentralPortfolio,当然,第一个元素没有变化。