-2

我有一個數組叫做used.I有一個函數,它在數組中添加一個代碼,但是檢查代碼是否被再次添加。如果它已被添加,它必須說「False」else True 問題是它會說錯誤和真實。 這裏是我的代碼:代碼將執行if和else語句。 Javascript

var used=[];//use that to check if the user has added the code to favourites again 

var x='',y='',blur='',r="",g="",b="",op="",color="rgb(0,0,0)",shadow=''; //create the variables that will be used 
function update(){ 
    x=$("#x").val(); 
    y=$("#y").val(); 
    op=$("#o").val(); 
    r=$("#red").val(); 
    g=$("#green").val(); 
    b=$("#blue").val(); 
    blur=$("#blur").val(); 
    shadow = x+"px "+y+"px "; 


} 
$(document).ready(function(){ 
$("#addC").click(function(){ 
    if(used.indexOf(shadow) != -1){ 
    alert("false"); 
    } 
    else{ 
    used.push(shadow); 
    alert("True"); 

    } 
}); 
    $('.shadow-slider').change(function(){ 

    update(); 

    if (op != 1){ 
    if(op.length > 3){op=op.substring(0, 3);} 
    color="rgba("+r+","+g+","+b+","+op+")"; 
    } 
    else{ 
    color="rgb("+r+","+g+","+b+")"; 
    } 
    if (blur != 0){shadow += blur+"px ";} 
    shadow += color; 
    $('#object').css("text-shadow",shadow); 
    $('#code-output').html("text-shadow:"+shadow+";"); 

    }); 



}); 

陰影是一個全局變量取決於我page.Any一些想法滑塊從而改變?

+0

您的點擊事件處理程序正在觸發兩次。 –

+0

它在哪裏觸發兩次,我什麼都看不到 – stranger4js

回答

4

我想你已經綁定的點擊處理器的兩倍,因此在第一次沒有找到,你會得到第二種情況(,並將其添加),但發現它的第二次(第一次添加 )並顯示第一個案例。