html4长连接,.NET_页面间隔半秒钟更新时间 Asp.net使用Comet开发http长连接示例分享,好处:1.和AJAX轮询比起来 节省 - phpStudy...

@{

Layout = null;

}

Index

var req = false;

var lastDelimiterPosition = -1;

$(document).ready(function () {

getData();

});

function getData() {

loadXMLDoc("/Comet/Test");

}

//新建一个XHR

function createRequest() {

if (window.XMLHttpRequest && !(window.ActiveXObject)) {

try {

req = new XMLHttpRequest();

} catch (e) {

req = false;

}        // branch for IE/Windows ActiveX version

} else if (window.ActiveXObject) {

try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {

try {

req = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e) {

req = false;

}

}

}

}

//发起请求

function loadXMLDoc(url) {

try {

if (req) {

req.abort();

req = false;

}

createRequest();

if (req) {

req.onreadystatechange = processReqChange;

req.open("GET", url, true);

req.send("");

} else {

alert('unable to create request');

}

} catch (e) { alert(e.message); }

}

//检查状态

function processReqChange() {

if (req.readyState == 3) {

try {

ProcessInput(req.responseText);

if (req.responseText.length > 3000) {

lastDelimiterPosition = -1; getData();

}

}

catch (e) {

alert(e.message);

}

}

}

//拆分字符串

function ProcessInput(input) {

var text = input;

var nextDelimiter = text.indexOf('|', lastDelimiterPosition + 1);

if (nextDelimiter != -1) {

var timeStamp = text.substring(nextDelimiter + 1);

if (timeStamp.length > 0) {

lastDelimiterPosition = nextDelimiter;

ProcessTime(timeStamp);

}

}

}

//输出 或者触发什么事件

function ProcessTime(time) {

document.getElementById('div1').innerHTML = time;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值