Skip to content

Commit

Permalink
更改接口调用方式
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 3, 2015
1 parent fb99591 commit df163a6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@

总之在`IE``Firefox`中一般情况下不能触发这俩事件,只有在选择脱机状态下才能触发此事件。

### 设置轮询时间和地址
在online.js中设置以下

> `time` 设置轮询时间
> `url``url`为空的时候 默认所有浏览器使用 onLine 和 offline事件
> - url 不为空的时候 IE 和 Firefox 使用 AJAX 轮询判断网络是否连接
> - url 请求的可以是服务器一个空php 等文件
> - 文件可以设置头文件,避免轮询跑更多的流量
### 引用 `online.js`

```html
<script type="text/javascript" src="online.js"></script>
```

### 侦听是否连上网络
### 设置轮询时间和地址

```js
window.onLineHandler = function(){
var net = onlinenetwork({
"time":1000,
"url":"http://*******.com/ping.php"
})
```

### 连上网络执行

```
net.onLineHandler(function(){
console.log("连上了!")
};
})
```

### 侦听是否断开网络
### 断开网络执行

```js
window.offLineHandler = function(){
net.offLineHandler(function(){
console.log("断开网络!")
};
})
```

51 changes: 37 additions & 14 deletions online.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
;(function(w){
var xmlhttp = new XMLHttpRequest(),
time=2000,//设置轮询时间
url="http://********.com/online.php";
//当 `url`为空的时候 默认所有浏览器使用 onLine 和 offline事件
url ="",//当 `url`为空的时候 默认所有浏览器使用 onLine 和 offline事件
OL = OL || {};

w.onlinenetwork = w.onlinenetwork || {};
w.onlinenetwork = {
OL={
OL_api:{//扩展API
onLineHandler:function(func){
onlinenetwork.online=func
return this
},
offLineHandler:function(func){
onlinenetwork.offline=func
return this
}
},
setStatus:function (newStatus) {
this.eventStatus(newStatus);
w.onLine = newStatus;
},
//状态改变执行事件
eventStatus: function (newStatus) {
if (newStatus === true && w.onLineHandler !== undefined && (w.onLine !== true || this.handlerFired === false)) {
w.onLineHandler();
if (newStatus === true && onlinenetwork.online !== undefined && (w.onLine !== true || this.handlerFired === false)) {
onlinenetwork.online();
}
if (newStatus === false && w.offLineHandler !== undefined && (w.onLine !== false || this.handlerFired === false)) {
w.offLineHandler();
if (newStatus === false && onlinenetwork.offline !== undefined && (w.onLine !== false || this.handlerFired === false)) {
onlinenetwork.offline();
}
this.handlerFired = true;
},
Expand All @@ -41,7 +50,7 @@
this.tryToSend(xmlhttp);
},
processXmlhttpStatus: function () {
var tempOnLine = w.onlinenetwork.verifyStatus(xmlhttp.status);
var tempOnLine = this.verifyStatus(xmlhttp.status);
this.setStatus(tempOnLine);
},
//尝试发送请求
Expand All @@ -62,7 +71,10 @@
},
//非 chrome 和 Safari 浏览器不停的检查,嘿嘿
startCheck:function(){
setInterval("window.onlinenetwork.XMLHttpLogic(true)",time);
var self = this
setInterval(function(){
self.XMLHttpLogic(true)
},time);
},
//第一次检查是否在线
checkOnLine:function(){
Expand All @@ -79,13 +91,15 @@
getExplorer: function(newStatus){
var explorer = window.navigator.userAgent;
this.setStatus(newStatus)
console.log(explorer)
console.log(url)
if((explorer.indexOf('Firefox') >= 0 || explorer.indexOf('MSIE') >= 0)&&url){
console.log("test:1")
console.log("getExplorer:1")
this.checkOnLine()
this.setStatus(newStatus)
this.startCheck(newStatus)
}else{
console.log("test:2")
console.log("getExplorer:2")
this.eventStatus(newStatus)
}
},
Expand All @@ -112,6 +126,15 @@
this.handlerFired = false;
}
}
w.onlinenetwork.init()


onlinenetwork=function (json){
if(json){
if (json.time) time=json.time;
if (json.url) url=json.url;
}
OL.init()
for (var a in OL.OL_api) this[a]=OL.OL_api[a];
return this
}
w.onlinenetwork=onlinenetwork
})(window);
15 changes: 11 additions & 4 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
<body>
<script type="text/javascript">

window.onLineHandler = function(){
var a = onlinenetwork({
"time":1000,
"url":"http://*******.com/ping.php"
})

a.onLineHandler(function(){
console.log("连上了!")
};
window.offLineHandler = function(){
})

a.offLineHandler(function(){
console.log("断开网络!")
};
})

</script>
</body>
</html>

0 comments on commit df163a6

Please sign in to comment.