나의 Winding Road

javascript/jQuery click event가 2의 제곱으로 늘어나는 문제 본문

개발/Web

javascript/jQuery click event가 2의 제곱으로 늘어나는 문제

WindingRoad 2016. 6. 20. 01:30

Art With 프로젝트


1. 문제 상황 : click event가 2의 제곱으로 늘어나는 현상이 생김.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function eachCatSecondLiClick() {
    $('#cat_div #ul_cat_second li a').each( function(index) {
        $(this).click( function() {
            var superCatName = $(this).text();
            //alert($(this).text());
            $('#cat_second_drop_down span').text(superCatName);
            loadNextCatList("cat_second_drop_down", superCatName);
            //changeHeightTransportDiv();
        });
    });
}
 
function loadNextCatList(superCat, superCatName) { // 카테고리 선택했을 경우
    $.ajax({
        url: "db/share_require/load_next_catlist.php"//the page containing php script
        type: "POST"//request type
        data: "superCat=" + superCat + "&;superCatName=" + superCatName,
        success: function (result) {
            //alert(result);
            var catNum = "cat_second_drop_down";
            //var catNum = "cat_third_drop_down";
            if(superCat == "cat_second_drop_down") {
                catNum = "cat_third_drop_down";
            }
            alert(catNum);
            emptyDropDown(catNum);
            console.log("loadNextCatList() called! " + result);
            var jsonObj = JSON.parse(result);
            setCatList(catNum, jsonObj);
            eachCatSecondLiClick();
        }
    });
}
cs


2. 해결 : 황당. 바보 같은 짓을 또 했다. 클릭 이벤트 설정해주는 함수가 2번씩 호출되니 당연히 2의 제곱으로 계속 늘어날 수 밖에... 다음에는 이렇게 바보 같은 짓 하지 말자는 의미로 기록.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function loadNextCatList(superCat, superCatName) { // 카테고리 선택했을 경우
    $.ajax({
        url: "db/share_require/load_next_catlist.php"//the page containing php script
        type: "POST"//request type
        data: "superCat=" + superCat + "&superCatName=" + superCatName,
        success: function (result) {
            var catNum;
            if(superCat == "cat_first_drop_down") {
                catNum = "cat_second_drop_down";
                emptyDropDown(catNum);
                console.log("loadNextCatList()1 called! " + result);
                var jsonObj = JSON.parse(result);
                setCatList(catNum, jsonObj);
                eachCatSecondLiClick();
            }
            //var catNum = "cat_third_drop_down";
            else if(superCat == "cat_second_drop_down") {
                catNum = "cat_third_drop_down";
                emptyDropDown(catNum);
                console.log("loadNextCatList()2 called! " + result);
                var jsonObj = JSON.parse(result);
                setCatList(catNum, jsonObj);
                //eachCatSecondLiClick();
            }
            else {
                /* 3번째 카테고리 클릭 이벤트 작성 필요 */
            }
        }
    });
}
cs

Comments