2013-02-05 2 views
0

더 빠르고 더 최적화 된 방법이 있습니까?더 빠르게이 작업을 수행하는 방법이 더 빠릅니까?

if (window.location.href.indexOf('/search/?') >= 0) { 
    if ($.cookie("Layout") == "Poster") { 
     $("link").attr("href", "../css/list.css"); 
    } else if ($.cookie("Layout") == "Poster") { 
     $("link:first").attr("href", "css/list.css"); 
    } 
} 

if (window.location.href.indexOf('/search/?') >= 0) { 
    if ($.cookie("Layout") == "Description") { 
     $("link").attr("href", "../css/desc.css"); 
    } else if ($.cookie("Layout") == "Description") { 
     $("link:first").attr("href", "css/desc.css"); 
    } 
} 
+2

테스트를 반복하지 않아도됩니다. –

+2

예, 확실히 최적화 할 수 있지만 실행 시간에는 눈에 띄는 차이가 없습니다. 그냥 유지하는 것이 더 쉽습니다. –

+0

당신이하고 싶은 것을 설명해야하며, 왜 읽을 수 있도록 코드를 포맷하고 포맷해야합니다. –

답변

0

실제로는 else if 절의 원래 조건과 동일한 내용을 테스트하고 있습니다. 이것은 중복됩니다. 같은 조건식 (자신은 단지 그것에 대해 생각 혼동)의 else 절에이기 때문에 else if 조건은 경우

if ($.cookie("Layout") == "Poster") { 
    ... 
}else if ($.cookie("Layout") == "Poster") { ... } 
뭔가 문제가 당신에게 소리한다

... 두 번째 .. 실행되지 않습니다 성명은 단순히 절대로 실행하지 않을 것입니다 ...

switch 문을 사용하는 것이 좋습니다. 이와 비슷한 것 -

if (window.location.href.indexOf('/search/?') >= 0) { 
    switch($.cookie("Layout")){ 
     case "Layout" : 
     // behavior for "Layout" 
     break; 
     case "Description" : 
     // behavior for "Description" 
     break; 
     default : 
     // behavior for unknown $.cookie("Layout") value 
     break; 
    } 
} 
관련 문제