내 js 코드 일부에서 이상한 동작이 나타납니다.클릭시 setTimeout 제거
일부 알림은 페이지 상단의 막대에 표시되고 일정 시간이 지나면 사라집니다. 나는 이것을 실현하기 위해 간단한 setTimeout()
을 사용했다.
가끔은 페이지가로드 될 때 특정 URL 쿼리 문자열의 결과로 알림이 표시되지만 사용자가 버튼을 클릭 할 때 새로운 URL이 표시되어야하는 경우도 있습니다. 나는 낡은 것을 사라지게하고 새 것을 나타 내기 바란다. 나는 그것을 취소하기 위해서 변수를 사용하여 setTimeout()
에 대한 참조를 유지하고 있습니다. 그러나이 작업을 수행하려고 할 때 루프를 만들어 내 크롬 탭이 충돌합니다. 다른 브라우저 탭을 충돌합니다 표시되는 동안 show notification
에 http://jsfiddle.net/5Nm4c/
클릭 -
나는 함께 내 문제를 설명하는 jsfiddle을 뒀다. 아무 것도 표시되지 않을 때 클릭하면 괜찮습니다. 이 요소는 뷰포트에서 볼 수 있습니다 있는지 확인하기 위해 몇 가지 좋은 방법이 있기 때문에 내가 https://github.com/ryanve/verge/을 사용하고
var Notification = {
// close main notification bar
close: function (callback) {
$('#notification-bar').fadeOut(250, function() {
// reset its position and fade it back in so it is ready to go again
$(this).css('top', -100).fadeIn(1);
// check if a callback function has been passed in
if (typeof callback === 'function') {
callback();
}
});
},
// open notification bar with the appropriate css class and message
open: function (message) {
// if the notification bar is already visisble
if (verge.inViewport($('#notification-bar'))) {
// hide and then show it with the new message
window.clearTimeout(Notification.timeout);
Notification.close(Notification.open(message));
return false;
}
$('#notification-bar').html(message);
$('#notification-bar').animate({
'top': 0
}, 250, function() {
Notification.timeout = window.setTimeout(function() { Notification.close() }, 1500);
});
},
timeout: null
}
Notification.open('hello');
$('#button').click(function(e){
e.preventDefault();
Notification.open('link clicked');
});
:
여기 내 JS입니다.
누군가 내 오류가 어디 있는지 말해 줄 수 있습니까?