대상 날짜를 설정하는 간단한 카운트 다운 타이머 인 첫 jQuery 플러그인을 작성하고 있습니다. 목표는 제공된 목표 날짜 & 시간까지 카운트 다운하는 일반 텍스트 시계를 얻는 것입니다. 내 인생에서 나는 실제로 카운트 다운 그래서 플러그인 내에서 setTimeout 또는 setInverval을 사용하는 방법을 알아낼 수 없습니다. stackoverflow 및 기타 소스를 통해 하루 종일 파고 들지만 솔루션을 찾을 수 없으므로 사과하지 않습니다. 여기 setTimeout 또는 setInterval을 사용하는 jQuery 플러그인
내가 가지고있는 작업은 다음과 같습니다.(function($){
$.fn.clock = function(options) {
// ESTABLISH DEFAULTS
var settings = $.extend({
'target' : '07/21/2013 09:00:00', // mm/dd/yyyy hh:mm:ss
}, options);
return this.each(function() {
// calculate milliseconds to target
tarDate = new Date(settings.target);
dateNow = new Date();
amount = tarDate.getTime() - dateNow.getTime();
delete dateNow;
// set label
label = settings.title;
// generate output
if(amount <= 0) {
out = '000:00:00:00';
} else {
td = 0; th = 0; tm = 0; ts = 0; out = ''; // reset everything
amount = Math.floor(amount/1000); // kill the milliseconds
td = Math.floor(amount/86400); // calculate days to target
amount = amount%86400; // convert amount to days
th = Math.floor(amount/3600); // calculate hours to target
amount = amount%3600; // convert amount to hours
tm = Math.floor(amount/60); // calculate minutes to target
amount = amount%60; // convert amount to minutes
ts = Math.floor(amount)+1; // calculate seconds to target
out += (td<=99?'0':'') + (td<=9?'0':'') + td + ':';
out += (th<=9?'0':'') + th + ':';
out += (tm<=9?'0':'') + tm + ':';
out += (ts<=9?'0':'') + ts;
}
// assemble and pump out to dom
$(this).html(out);
// set refresh rate
??????
});
};
})(jQuery);
어떻게 작동하는지 알 수 있지만 updateClock 함수에 전달되는 플러그인 옵션은 어떻게 얻을 수 있습니까? $ .fn.clock에서 setTimeout을 사용할 수 있습니까? 그건 그렇고 좋은 기사, 링크 주셔서 감사합니다. – codybuell
@codybuell 내 업데이트 된 답변보기. 기본적으로 요소를 만들 때 옵션을 저장 한 다음 메서드를 호출 할 때'data' 함수를 사용하여이를 검색합니다 (개별 시계에서 사용되는 경우 상태를 유지하는 데에도 사용할 수 있음). 요소를 만든 직후 처음으로 update funcion을 호출 할 수 있으며, 결국 당신을 위해'setTimeout'을 호출 할 것입니다. 아니면 그냥 직접 전화해라. – mgibsonbr