binding an event in offline.js

拟墨画扇 提交于 2019-12-13 18:27:39

问题


I'm trying to get offline.js working together with toastr alerts.

My limited JS knowledge is holding me back but hoping some can shed some light on this.

What I want to happen When the connection goes from down to up, I want to display a "re-connected successfully" message in a toast alert.

The Problem I don't understand how to check for that status in offline.js. The docs mention that it's possible using this:

Offline.on(event, handler, context) : Bind an event. Events:
up: The connection has gone from down to up
down: The connection has gone from up to down

the up event does what i want but i can't figure out how to put it in to practice...

Below is really just some psuedocode explaining what i'd like to happen:

function checkifbackonline(){
      var backonlinemessage = "re-connected successfully";
      var checkstate = Offline.on(event, handler, context);
      if (checkstate = true) {
        toastr.info(backonlinemessage);
      }
    }
window.setInterval(checkifbackonline, 3000);

Can anyone put me on the right path?


回答1:


you have not attached up event as per the documentation.

function checkifbackonline(evt){
    var backonlinemessage = "re-connected successfully";
    toastr.info(backonlinemessage);
}
Offline.on("up", checkifbackonline);

when up is triggerd it will call checkifbackonline function. If you want to remove the event binding then call Offline.off("up");



来源:https://stackoverflow.com/questions/29411422/binding-an-event-in-offline-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!