1. 程式人生 > >jquery ajax監聽事件

jquery ajax監聽事件

var oldComparePeriod = "";
var newComparePeriod = "";
$(function(){
	
	//將新的期數值放進去
	newComparePeriod = $('#comparePeriod').combobox('getValue');
	//啟動監聽器
	setInterval("ajaxListener()",5000);//每5秒監聽一次
		
});
// 監聽器
function ajaxListener(){
	newComparePeriod = $('#comparePeriod').combobox('getValue');
	//期數change時向後臺傳送請求
	if(newComparePeriod == oldComparePeriod)
		return ;
	oldComparePeriod = newComparePeriod;
	var data = {comparePeriod:oldComparePeriod};
	jQuery.ajax({ //傳送請求
    	type: 'post', 
        url : 'selectComparePeriod.htm',
  		dataType:'json', 
        data: data, 
        success: function(rs){
            if(rs==null || rs.length==0){
                return ;
            }else{
				var period = rs[0];
				var beginNearDate = period.beginNearDate;
				var beginDate = period.beginDate;
				var endDate = period.endDate;
				document.getElementById('beginNearDate').innerText=beginNearDate;
				document.getElementById('beginDate').innerText=beginDate;
				document.getElementById('endDate1').innerText=endDate;
				document.getElementById('endDate2').innerText=endDate;
				/*
				$('#beginNearDate').textbox('setValue',beginNearDate);
				$('#beginDate').textbox('setValue',beginDate);
				$('#endDate1').textbox('setValue',endDate);
				$('#endDate2').textbox('setValue',endDate);
				*/
			}	
        },
    	error:function(){
    		jQuery.messager.alert('提示', '伺服器正忙,請稍後重試。');//異常提示
    	}
   });
}