2013-10-11 2 views
0

Ajax 요청 후 일부 데이터를 저장하기 위해 전역 JavaScript 변수를 사용하고 있습니다. jqueryui의 날짜 선택기 요일을 표시하는 데 사용하고,이 내가 두 로그 디버깅을위한 배열은배열의 길이를 0으로 설정하면 배열이 비어있는 것처럼 보입니다.

function get_date_exceptii(data, cod_calendar) { 

if(typeof(window.date_zile_ore_modificate) !== 'undefined' && typeof(window.date_zile_nelucratoare) !== 'undefined') { 
    window.date_zile_ore_modificate.length = 0; 
    window.date_zile_nelucratoare.length = 0; 
} 
else { 
    window.date_zile_ore_modificate = []; 
    window.date_zile_nelucratoare = []; 
} 

parametri = 'cod_calendar='+cod_calendar+'&data='+data; 

$j.ajax({ 
    url: "proiecte/ajax/colectare_date_exceptii.php?sid="+Math.random(), 
    type: 'POST',  
    async: false, 
    data: parametri    
}) 
.done(function (msg) { 
    arr_msg = msg.split('[sep1]'); 
    $j.each(arr_msg, function (index, val) { 
     arr_exceptie = val.split('[sep]'); 
     if(arr_exceptie[1] == 'nelucratoare') 
      window.date_zile_nelucratoare[arr_exceptie[0]] = arr_exceptie[2]; 
     else { 
      window.date_zile_ore_modificate[arr_exceptie[0]] = arr_exceptie[2]; 
     } 
    }); 
}); 
console.log(window.date_zile_ore_modificate); 
console.log(window.date_zile_nelucratoare); 
} 

을 계산하도록 사용하고 기능 중이다 array[date] = type_of_date

형태로 저장된 코스. 문제는 코드 섹션이 실행 되어도 배열이 지워지지 않는다는 것입니다. 내 구문이 잘못 되었습니까? 아니면 전역 변수 및/또는 여기에 대한 배열을 잘못 이해하고 있습니까?

+6

왜'date_zile_ore_modificate = []'? 또한, 나는 '창'을 본 적이 없다고 생각합니다. 변수에 대한 스크립트에서 너무 많이 사용되었습니다. 정말 필요합니까? 복잡한 범위 지정 문제가 있습니까? – SmokeyPHP

+2

'array.length = 0'은 괜찮을 것입니다. 그러나 개인적으로 @SmokeyPHP가 말한 것과 같이 가겠습니다 – Archer

+0

[자바 스크립트에서 배열을 비우십시오 ...] (http://stackoverflow.com/a/1232046/1823841) –

답변

4

arr_exceptie[0]으로 지정된 색인은 숫자가 아닌 것으로 보입니다. 숫자가 아닌 이름을 가진 속성이 해제되지 않도록 length = 0 설정

만 (ES5 15.4.5.2 당) 수치-명명 된 속성을 지 웁니다. 예를 들어 date_zile_ore_modificate['foo']date_zile_ore_modificate[5]을 설정 한 경우 date_zile_ore_modificate.length=0을 설정하면 5 속성이 지워지지만 foo 속성은 지워지지 않습니다.

그러나 변수를 완전히 새로운 배열로 설정하면 분명히 모든 속성이 지워집니다. 이전 배열 개체를 완전히 제거하기 때문입니다. 따라서 모든 경우에

window.date_zile_ore_modificate = []; 
window.date_zile_nelucratoare = []; 

을 사용하고 초기 조건을 제거하십시오. 당신의 속성 이름은 전체 숫자가 아닌 경우 (배열은 숫자 인덱스 데이터를 정말에만 적합하기 때문에)

또한, 당신은 아마 대신 배열의 일반 객체를 사용한다 :

window.date_zile_ore_modificate = {}; 
window.date_zile_nelucratoare = {}; 
+0

그래,이 문제는 하나만 있습니다. IE에서'window.date_zile_ore_modificate = [];'시도 할 때마다 "정의되지 않은"오류가 발생합니다. – Bogdan

+0

편집, 신경 쓰지 마라. 정의되지 않은 오류는 개발자 도구가 비활성화 된 상태에서 IE에서 console.log를 사용하려고하는 것으로부터 왔습니다. – Bogdan

관련 문제