-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTML5 #8
Comments
|
MutationObserverMutationObserver可以用于检测dom的变化,当dom发生变化时会触发回调函数。需要注意的是,为了防止dom频繁变化时调用回调函数引起卡顿现象,MutationObserver 会将dom的一些变动存于数组中统一处理,并不是只要dom发生变化就执行回调函数。 MutationObserver 构造的实例对象需要设置一个回调函数,当dom发生变化时会调用这个函数,如下: var observer = new WebKitMutationObserver(
function callback
); MutationObserve 实例对象进行监听需要调用 observe 方法,observe 方法的需要的参数有两个, 目标dom节点和配置信息,如下: void observe(
Node target,
MutationObserverInit options
); options 配置可有如下选项:
以下是完整的实例: var observer = new WebKitMutationObserver(function(mutations) {
// mutations 是 dom 多次变动信息
mutations.forEach(function(mutation) {
var addedNodes = mutation.addedNodes;
for (var i = 0; i < addedNodes.length; i++) {
var nodeTagName = addedNodes[i].tagName;
console.log(nodeTagName);
}
});
});
var observerConfig = {
childList: true,
};
var targetNode = document.getElementById('#node');
observer.observe(targetNode, observerConfig); 参考文档: |
Server-Send Events服务器推送事件(Server-sent Events)是 HTML 5 规范中的一个组成部分,可以用来从服务端实时推送数据到浏览器端。它的主要特点是,数据传送是单向的,只能从服务端向浏览器推送数据,浏览器可以订阅服务端推送来的数据。 var http = require('http');
var sys = require('sys');
var fs = require('fs');
http.createServer(function(req, res) {
//debugHeaders(req);
if (req.headers.accept && req.headers.accept == 'text/event-stream') {
if (req.url == '/events') {
sendSSE(req, res);
} else {
res.writeHead(404);
res.end();
}
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(fs.readFileSync(__dirname + '/sse-node.html'));
res.end();
}
}).listen(8000);
function sendSSE(req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
var id = (new Date()).toLocaleTimeString();
// Sends a SSE every 5 seconds on a single connection.
setInterval(function() {
constructSSE(res, id, (new Date()).toLocaleTimeString());
}, 5000);
constructSSE(res, id, (new Date()).toLocaleTimeString());
}
function constructSSE(res, id, data) {
res.write('id: ' + id + '\n');
res.write("data: " + data + '\n\n');
}
function debugHeaders(req) {
sys.puts('URL: ' + req.url);
for (var key in req.headers) {
sys.puts(key + ': ' + req.headers[key]);
}
sys.puts('\n\n');
} <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script>
var source = new EventSource('/events');
source.onmessage = function(e) {
document.body.innerHTML += e.data + '<br>';
};
</script>
</body>
</html>
~ |
Page Visibility
当用户进行最小化、切换tab、锁屏等操作离开当前页面之后,这时用户虽然不再关注这个页面,但是页面上的一些行为依然会像之前一样执行。例如一个视频播放页,将其最小化之后,视频如果依然播放下去,这无疑会造成资源的浪费。如果开发人员能够获得页面的是否可见的状态,就可以采取相应的策略取消或更改页面的行为,为此 HTML 提供了Page Visibility。
Page Visibility 是一个用于检查页面是否处于可见 (visibility) 状态的 API. 它提供了两个属性,
document.hidden
和document.visibilityState
。当页面处于激活状态document.hidden
的值为false
, 否则 它的值为true
.document.visibilityState
可以是以下值:另外,还提供了一个事件
visibilitychange
,当页面可见状态发生变化,就会触发visibilitychange
事件。Page Visibility 好处就是节省资源.
更多资源
http://www.w3.org/TR/page-visibility/?csw=1#dom-document-visibilitystate
http://www.html5rocks.com/en/tutorials/pagevisibility/intro/
https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
http://blog.teamtreehouse.com/an-introduction-to-the-page-visibility-api
amfe/article#26
The text was updated successfully, but these errors were encountered: