0

我正在尝试制作一个非常简单的脚本,该脚本应该让我登录一个站点,在 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”功能?

4

1 回答 1

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

我对此并不完全确定,但我猜想通过中止请求会中止数据的下载和检索,但会触发所有其他状态。我尝试使用未缓存的大图像并且请求很快完成,没有任何结果。

BTW. To just send a »ping« to your server you can also set the src of an image tag to the desired script, this will trigger the request too.

于 2013-10-26T16:41:38.957 回答