2012-12-17 2 views
0

내가하려는 것은 3 초 후에 페이지가로드되고 아무것도 나타나지 않으면 3 초 후에 부분적으로 숨겨집니다. 이제 커서가 상자에 들어가면 제한 시간이 지워지고 광고가 숨겨지지 않습니다.settimeout이 지워지지 않습니다

마우스를 놓고 다시 들어가면 이전 제한 시간이 그대로 유지됩니다. 시간 제한을 지우려고하지만 상자는 여전히 숨겨져 있습니다. 무엇이 문제 일 수 있습니까? (JSfiddle 링크 : http://jsfiddle.net/aK9nB/)

var pstimer; 

$(document).ready(function(){ 
    setTimeout(function(){ 
     showps(); 
     pstimer = setTimeout(function() { 
       hideps(); 
     }, 3000); 
    }, 3000); 
}); 

$('#psclose').on('click', function(){ 
    $('#postsearch-container').hide(); 
}); 

$("#postsearch-container").hover(
    function() { 
     console.log("enter"); 
     clearTimeout(pstimer); 
     console.log("cleartimeout"); 
     showps(); 
    }, 
    function() { 
     console.log("leave"); 
     clearTimeout(pstimer); 
     var pstimer = setTimeout(function(){ 
      hideps(); 
    } , 3000); 
}); 

function showps() { 
    $("#postsearch-container").stop(); 
    $('#postsearch-container').animate({ 
     bottom: '0' 
    }, 'slow'); 
} 

function hideps() { 
    $('#postsearch-container').animate({ 
     bottom: '-115' 
    }, 'slow'); 
} 
+6

'var pstimer'는 범위 밖에서 접근 할 수없는 새로운 함수 범위 변수를 선언하고 있습니다. 귀하가 선언 한 세계를 ** 설정하지 ** 않습니다. – Shmiddty

답변

2
$("#postsearch-container").hover(
    function() { 
     console.log("enter"); 
     clearTimeout(pstimer); 
     console.log("cleartimeout"); 
     showps(); 
    }, 
    function() { 
     console.log("leave"); 
     clearTimeout(pstimer); 
     pstimer = setTimeout(function(){ // remove the "var" 
      hideps(); 
     } , 3000); 
    } 
); 
+0

고마워요. 곧 답변을 받아 들일 것입니다. – vivek

2

pstimer 앞에 VAR을 제거하려고

내 코드를 참조하십시오. VAR를 사용

function() { 
     console.log("leave"); 
     clearTimeout(pstimer); 
     /* var */ pstimer = setTimeout(function(){ 
      hideps(); 
     } , 3000); 
    } 

는 의도 pstimer와 이름을 공유하는 새로운 로컬 변수를 정의하지만,이 함수 호출 내에서만 사용할 수 있습니다. 함수가 완료되면, 로컬 변수는 파괴된다.

관련 문제