2008년 1월 9일 수요일

JAVA SCRIPT(AJAX) - DINAMIC TIME DISPLAY


보여줄 부분


<form name="counter">
<div id="displayTime"></div>
</form>




스크립트


<SCRIPT type="text/javascript">


var xmlHttp;

function createXMLHttpRequest() {
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}

if(!xmlHttp)
alert("fail connect");
return;
}

function startRequest() {
createXMLHttpRequest();
xmlHttp.open ("GET", "http://stillrabbit/systime.jsp", true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.send(null);
}


function handleStateChange() {
if (xmlHttp.readyState == 4)
if (xmlHttp.status == 200){
reverse_counter(xmlHttp.responseText);
}
else if (xmlHttp.status == 404)
alert("Request URL does not exist");
else
alert("Error: status code is " + xmlHttp.status);
}
function reverse_counter(systime){

today = toTimeObject(systime.substring(6,20));

d_day = new Date(<%= 지정된 날짜 %>);
//ex (2008,11,11,00,00)
days = (d_day - today) / 1000 / 60 / 60 / 24;



daysRound = Math.floor(days);
hours = (d_day - today) / 1000 / 60 / 60 - (24 * daysRound);

hoursRound = Math.floor(hours);
minutes = (d_day - today) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
minutesRound = Math.floor(minutes);
seconds = (d_day - today) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) -
(60 * minutesRound);
secondsRound = Math.round(seconds);
sec = " 초"
min = " 분 "
hr = " 시간 "
dy = " 일 "

//다이나믹 텍스트 출력 부분
//document.counter.recom_top_table02_data02_05.value = daysRound +
//dy + hoursRound + hr + minutesRound + min + secondsRound + sec;

//폼에 출력부분
if(eval(daysRound) < 0){
document.all["displayTime"].innerHTML = "경매종료";
}else{
document.all["displayTime"].innerHTML = daysRound + dy + hoursRound + hr + minutesRound + min + secondsRound + sec;
}

// newtime =
}
function set_timer(){
startRequest();
newtime=window.setTimeout("set_timer();", 1000);

}

/////////////////////////////////////////////////////
function toTimeObject(time) { //parseTime(time)
/////////////////////////////////////////////////////
// Time 스트링을 자바스크립트 Date 객체로 변환 parameter time: Time 형식의 String
var year = time.substr(0,4);
var month = time.substr(4,2) - 1; // 1월=0,12월=11
var day = time.substr(6,2);
var hour = time.substr(8,2);
var min = time.substr(10,2);
var ss = time.substr(12,2);

return new Date(year,month,day,hour,min,ss);
}




시간을 반환하는 sysTime.jsp


<%@page contentType="text/html;charset=euc-kr" session="true" %>
<%@page import="java.text.SimpleDateFormat,java.util.*"%>
<%
java.util.Date toDate = null;
String nowDate = "";
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMddHHmmss" );
String curDate = formatter.format(cal.getTime());
toDate = formatter.parse( curDate );
//nowDate = curDate.substring(0,4)+","+(Integer.parseInt(curDate.substring(4,6))-1)+","+curDate.substring(6,8)+","+curDate.substring(8,10)+","+curDate.substring(10,12)+","+curDate.substring(12,14);
//nowDate = curDate.substring(0,4)+","+(Integer.parseInt(curDate.substring(4,6))-1)+","+curDate.substring(6,8)+","+curDate.substring(8,10)+","+curDate.substring(10,12)+curDate.substring(12,14);
%>
<%=curDate%>

댓글 없음: