在offline.js中绑定事件 [英] binding an event in offline.js

查看:129
本文介绍了在offline.js中绑定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 offline.js 我有限的JS知识使我受挫,但希望能对此有所启发.

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.

问题 我不明白如何在offline.js中检查该状态.文档中提到可以使用以下方法:

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(事件,处理程序,上下文):绑定事件.活动:
向上:连接已从向下变为向上
down:连接从上到下

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

up事件完成了我想要的,但我不知道如何进行练习...

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?

推荐答案

您尚未按照文档附加事件.

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

触发启动时,它将调用checkifbackonline函数.如果要删除事件绑定,请调用Offline.off("up");

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

这篇关于在offline.js中绑定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆