use newer function syntax

This commit is contained in:
Zeke Sikelianos 2016-10-03 16:40:51 -07:00
parent 253a383168
commit eaeebd3ebd

View file

@ -1,35 +1,36 @@
# 在线/离线事件探测 # 在线/离线事件探测
使用标准 HTML5 APIs 可以实现在线和离线事件的探测,就像以下例子: 使用标准 HTML5 APIs 可以实现在线和离线事件的探测,就像以下例子:
*main.js* _main.js_
```javascript
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
var onlineStatusWindow ```javascript
app.on('ready', function () { const {app, BrowserWindow} = require('electron')
let onlineStatusWindow
app.on('ready', () => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false }) onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`) onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
}) })
``` ```
*online-status.html* _online-status.html_
```html ```html
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body> <body>
<script> <script>
var alertOnlineStatus = function() { const alertOnlineStatus = () => {
window.alert(navigator.onLine ? 'online' : 'offline'); window.alert(navigator.onLine ? 'online' : 'offline')
}; }
window.addEventListener('online', alertOnlineStatus); window.addEventListener('online', alertOnlineStatus)
window.addEventListener('offline', alertOnlineStatus); window.addEventListener('offline', alertOnlineStatus)
alertOnlineStatus(); alertOnlineStatus()
</script> </script>
</body> </body>
</html> </html>
``` ```