2013-02-13 6 views
3

누군가이 오류를 왜 내게 말해 줄 수 있습니까? settimeout Uncaught ReferenceError : 함수가 정의되지 않았습니다.

는 나는 (성가신 얻고 있었다) 너무 민감하지 그래서 내가 그것을 연기 할 수 있도록 기능에 코드를 이동

catch되지 않은 ReferenceError가 : showleftnav가 정의되어 있지

: hideleftnav는

catch되지 않은 ReferenceError가

을 정의되지 않은
function showleftnav() 
    { 
     $(".leftnavdiv").css('width','500px'); 
     $("body").css('padding-left','510px'); 
     //get measurements of window 
     var myWidth = 0, myHeight = 0; 
     if(typeof(window.innerWidth) == 'number') { 
      //Non-IE 
      myWidth = window.innerWidth; 
      myHeight = window.innerHeight; 
     } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { 
      //IE 6+ in 'standards compliant mode' 
      myWidth = document.documentElement.clientWidth; 
      myHeight = document.documentElement.clientHeight; 
     } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) { 
      //IE 4 compatible 
      myWidth = document.body.clientWidth; 
      myHeight = document.body.clientHeight; 
     } 
     $('#maindiv').width(myWidth - 540); 
    } 

    function hideleftnav() 
    { 
     $(".leftnavdiv").width(10); 
     $("body").css('padding-left','20px'); 
     //get measurements of window 
     var myWidth = 0, myHeight = 0; 
     if(typeof(window.innerWidth) == 'number') { 
      //Non-IE 
      myWidth = window.innerWidth; 
      myHeight = window.innerHeight; 
     } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { 
      //IE 6+ in 'standards compliant mode' 
      myWidth = document.documentElement.clientWidth; 
      myHeight = document.documentElement.clientHeight; 
     } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) { 
      //IE 4 compatible 
      myWidth = document.body.clientWidth; 
      myHeight = document.body.clientHeight; 
     } 
     $('#maindiv').width(myWidth - 50); 
    } 

    $(".leftnavdiv").live({           //code for autohide 
     mouseenter: 
     function() { 
      setTimeout("showleftnav()", 5000); 
     }, 
     mouseleave: 
     function() { 
      setTimeout("hideleftnav()", 5000); 
     } 
    }); 

답변

8

setTimeout 문자열을 첫 번째 인수로 사용하는 것과 관련된 한 가지 문제점을 발견했습니다.

(function() { 
    function test() { 
     console.log('test'); 
    } 

    setTimeout('test()', 500); // ReferenceError: test is not defined 
    setTimeout(test, 500);  // "test" 
    setTimeout(function() {  // "test" 
     test(); 
    }), 500); 
})(); 

데모 : http://jsfiddle.net/mXeMc/1/

문자열을 사용하면 window 컨텍스트를 평가하는 코드를 일으키는 여기에 같은 문제를 설명하는 응축 예입니다. 그러나 코드가 콜백 함수에 있으므로 testwindow에서 액세스 할 수 없습니다. 비공개이며 익명 기능의 범위에만 제한됩니다.

test으로 함수를 참조하면 eval을 사용하지 않고 함수를 직접 가리키므로이 문제를 피할 수 있습니다.

+0

'$ (document) .ready (function() {}'(jquery 파일) 위에 함수를 이동시킨 다음 효과가있었습니다. 도움을 주셔서 감사합니다 : –

+0

@KellyLarsen : 그냥'setTimeout ("showleftnav()", 5000);'setTimeout (showleftnav, 5000);' – Blender

관련 문제