Javascript 只需获取循环的最后一个值并停止函数的执行
我有一个颜色选择器,拖动时,它会连续调用函数Javascript 只需获取循环的最后一个值并停止函数的执行,javascript,jquery,loops,if-statement,Javascript,Jquery,Loops,If Statement,我有一个颜色选择器,拖动时,它会连续调用函数hello(e,2)。我正在传递第二个变量2,这样第一个if语句将不会执行。现在,关于持续变化,我不想这样 list[2] =list[1]; list[1]=list[0]; list[0]=e; 这应该只在最后一次执行。所以这些地方只移动一次。我希望你能理解我的问题。这是我的职责 function hello(e, a) { if (a == 1 && e != list[0] && e != list[1
hello(e,2)
。我正在传递第二个变量2,这样第一个if语句将不会执行。现在,关于持续变化,我不想这样
list[2] =list[1];
list[1]=list[0];
list[0]=e;
这应该只在最后一次执行。所以这些地方只移动一次。我希望你能理解我的问题。这是我的职责
function hello(e, a) {
if (a == 1 && e != list[0] && e != list[1]) {
list[2] = list[1];
list[1] = list[0];
list[0] = e;
var strContent = "";
for (var i = 0; i <= 2; i++) {
strContent += "<div class=\"pick\" style=\"background-color:" + list[i] + "\" onclick=\"hello(this.style.backgroundColor,0);\"></div>";
}
}
if (a == 2 && e != list[0] && e != list[1]) {
list[0] = e;
var strContent = "";
for (var i = 0; i <= 2; i++) {
strContent += "<div class=\"pick\" style=\"background-color:" + list[i] + "\" onclick=\"hello(this.style.backgroundColor,0);\"></div>";
}
}
$('#colorpick').html(strContent);
//clr = 'rgb('+r+','+g+','+b+')';
clr = e;
var rgb = clr.replace(/^(rgb|rgba)\(/, '').replace(/\)$/, '').replace(/\s/g, '').split(',');
myColor.r = parseInt(rgb[0]);
myColor.g = parseInt(rgb[1]);
myColor.b = parseInt(rgb[2]);
curColor = myColor;
document.getElementById('color-lib-1').style.display = "none";
document.getElementById('color-lib-2').style.display = "none";
document.getElementById('color-lib-3').style.display = "none";
document.getElementById('color-lib-4').style.display = "none";
}
函数hello(e,a){
如果(a==1&&e!=列表[0]&&e!=列表[1]){
列表[2]=列表[1];
列表[1]=列表[0];
列表[0]=e;
var strContent=“”;
对于(var i=0;i来说,很难猜出您在那里试图做什么,主要是因为我确信它本可以以更好的方式完成
function hello(e) {
if(e === list[0] && e === list[1]){
return false;
}else{
list.unshift(e);
}
var strContent = "";
for (var i = 0; i <= 2; i++) {
strContent += "<div class=\"pick\" style=\"background-color:" + list[i] + "\" onclick=\"hello(this.style.backgroundColor,0);\"></div>";
}
//followed by whatever code
}
函数hello(e){
如果(e==列表[0]&&e==列表[1]){
返回false;
}否则{
列表.取消移位(e);
}
var strContent=“”;
对于(var i=0;i),这是不可读的。请正确缩进您的代码。我希望您理解我的问题。我读了三次,但无法理解。因此,您的意思是不希望将“2”作为“a”的值传递你最后一次调用它?我认为这超出了你向我们展示的函数的控制范围。函数本身不知道这是你第一次调用它还是第一百万次调用它。