binding an event in offline.js

2019-08-15 17:14发布

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条回答
2楼-- · 2019-08-15 17:41

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");

查看更多
登录 后发表回答