我正在尝试创建一个非常简单的脚本,该脚本应该让我在一个站点上登录,在10分钟不活动后删除会话。这很简单,如下:
//Silently "refresh" the page - at least server thinks you refreshed, thus you're active
function call() {
var req = new XMLHttpRequest();
//Load current url
req.open("GET",location.href,true);
//Try to only send the data! (does not work - the browser also receives data)
req.onprogress = function() {
this.abort(); //Abort request
wait(); //Wait another 5 minutes
}
//Repeat request instantly if it fails
req.onerror = call;
//Send
req.send();
}
function wait() {
//5minutes timeout
setTimeout(call,5000);
}
wait();
这完美无缺,但请求似乎完全加载。虽然页面很小,但我想让它干净并防止下载数据。这意味着,我想在数据开始下载后立即停止加载。或者更好 - 在发送数据之后。
有没有办法制作这种“ping”功能?
答案 0 :(得分:0)
我试过这段代码:
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
console.log( this.readyState );
if( this.readyState == 2 ) { //sent, otherwise it raises an error
console.log( 'aborting...' );
this.abort()
}
if( this.readyState == 4 ) {
console.log( this.responseText );
}
}
req.open( 'get', .... );
req.send();
打印:
1
2
aborting...
3
4
undefined
我对此并不完全确定,但我想通过中止请求会中止下载和检索数据,但会触发所有其他状态。我尝试使用没有缓存的大图像,并且请求很快完成,没有任何结果。
顺便说一句。要向服务器发送“ping”,您还可以将图像标记的src
设置为所需的脚本,这也会触发请求。