基本動畫循環javascript
(function(){ function updateAnimations(){ doAnimation1(); doAnimation2(); //其餘動畫 } setInterval(updateAnimations, 100); })();
幾個瀏覽器的計時器精度php
mozRequestAnimationFrame() 方法接收一個參數,即在重繪屏幕前調用的一個函數。這個函數負責改變下一次重繪時的 DOM樣式。爲了建立動畫循環,能夠像之前使用 setTimeout() 同樣,把多個對mozRequestAnimationFrame() 的調用連綴起來html
function updateProgress(){ var div = document.getElementById("status"); div.style.width = (parseInt(div.style.width, 10) + 5) + "%"; if (div.style.left != "100%"){ mozRequestAnimationFrame(updateProgress); } } mozRequestAnimationFrame(updateProgress);
(function(){ function draw(timestamp){ //計算兩次重繪的時間間隔 var drawStart = (timestamp || Date.now()), diff = drawStart - startTime; //使用 diff 肯定下一步的繪製時間 //把 startTime 重寫爲這一次的繪製時間 startTime = drawStart; //重繪 UI requestAnimationFrame(draw); } var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame, startTime = window.mozAnimationStartTime || Date.now(); requestAnimationFrame(draw); })();
三部分組成java
document.visibilityState,表示下列 4 個可能狀態的值。git
function handleVisibilityChange(){ var output = document.getElementById("output"), msg; if (document.hidden || document.msHidden || document.webkitHidden){ msg = "Page is now hidden. " + (new Date()) + "<br>"; } else { msg = "Page is now visible. " + (new Date()) + "<br>"; } output.innerHTML += msg; } //要爲兩個事件都指定事件處理程序 EventUtil.addHandler(document, "msvisibilitychange", handleVisibilityChange); EventUtil.addHandler(document, "webkitvisibilitychange", handleVisibilityChange);
navigator.geolocation對象包含3個方法web
getCurrentPosition(),接收3個參數:成功回調函數、可選的失敗回調函數、可選的選項對象。瀏覽器
成功回調函數,有兩個屬性:coords和timestamp。其中coords包含相關信息服務器
失敗回調函數,在被調用的時候接收一個參數,這個參數是一個對象,包含兩個屬性:message和code。其中message屬性中包含文本信息,解釋爲何會出錯,code中保存着一個值,表示錯誤的類型:用戶拒絕共享一、位置無效二、超時3app
navigator.geolocation.getCurrentPosition(function(position){ drawMapCenteredAt(position.coords.latitude, positions.coords.longitude); }, function(error){ console.log("Error code: " + error.code); console.log("Error message: " + error.message); })
選項對象,用於設定信息的類型。能夠設置的選項有三個:enableHightAccurancy是一個布爾值,表示必須儘量使用最準確的位置信息;timeout是以毫秒數表示的等待位置信息的最長時間;maximumAge表示上一次取得座標信息的有效時間,毫秒錶示dom
navigator.geolocation.getCurrentPosition(function(position){ drawMapCenteredAt(position.coords.latitude, positions.coords.longitude); }, function(error){ console.log("Error code: " + error.code); console.log("Error message: " + error.message); }, { enableHighAccuracy: true, timeout: 5000, maximumAge: 25000 })
每一個File對象都有下列只讀屬性
爲了讀取文件中的數據,FileReader提供的方法
var filesList = document.getElementById("files-list"); EventUtil.addHandler(filesList, "change", function(event){ var info = "", output = document.getElementById("output"), progress = document.getElementById("progress"), files = EventUtil.getTarget(event).files, type = "default", reader = new FileReader(); if (/image/.test(files[0].type)){ reader.readAsDataURL(files[0]); type = "image"; } else { reader.readAsText(files[0]); type = "text"; } reader.onerror = function(){ output.innerHTML = "Could not read file, error code is " + reader.error.code; }; reader.onprogress = function(event){ if (event.lengthComputable){ progress.innerHTML = event.loaded + "/" + event.total; } }; reader.onload = function(){ var html = ""; switch(type){ case "image": html = "<img src=\"" + reader.result + "\">"; break; case "text": html = reader.result; break; } output.innerHTML = html; }; });
File對象還支持slice()方法,這個方法接收兩個參數:起始的字節及要讀取的字節數
function blobSlice(blob, startByte, length){ if (blob.slice){ return blob.slice(startByte, length); } else if (blob.webkitSlice){ return blob.webkitSlice(startByte, length); } else if (blob.mozSlice){ return blob.mozSlice(startByte, length); } else { return null; } }
Blob類型有一個size屬性和一個type屬性,支持slice()方法,以便進一步切割數據
var filesList = document.getElementById("files-list"); EventUtil.addHandler(filesList, "change", function(event){ var info = "", output = document.getElementById("output"), progress = document.getElementById("progress"), files = EventUtil.getTarget(event).files, reader = new FileReader(), blob = blobSlice(files[0], 0, 32); if (blob){ reader.readAsText(blob); reader.onerror = function(){ output.innerHTML = "Could not read file, error code is " + reader.error.code; }; reader.onload = function(){ output.innerHTML = reader.result; }; } else { alert("Your browser doesn' t support slice()."); } });
使用對象URL的好處是能夠沒必要把文件內容讀取到JavaScript中而直接使用文件內容,爲此只要在須要文件內容的地方提供對象URL便可,要建立對象URL,能夠使用window.URL.createObjectURL()方法,並傳入File或Blob對象
var filesList = document.getElementById("files-list"); EventUtil.addHandler(filesList, "change", function(event){ var info = "", output = document.getElementById("output"), progress = document.getElementById("progress"), files = EventUtil.getTarget(event).files, reader = new FileReader(), url = createObjectURL(files[0]); if (url){ if (/image/.test(files[0].type)){ output.innerHTML = "<img src=\"" + url + "\">"; } else { output.innerHTML = "Not an image."; } } else { output.innerHTML = "Your browser doesn't support object URLs."; } });
var droptarget = document.getElementById( "droptarget"); function handleEvent(event){ var info = "", output = document.getElementById("output"), files, i, len; EventUtil.preventDefault(event); if (event.type == "drop"){ files = event.dataTransfer.files; i = 0; len = files.length; while (i < len){ info += files[i].name + " (" + files[i].type + ", " + files[i].size + " bytes)<br>"; i++; } output.innerHTML = info; } } EventUtil.addHandler(droptarget, "dragenter", handleEvent); EventUtil.addHandler(droptarget, "dragover", handleEvent); EventUtil.addHandler(droptarget, "drop", handleEvent);
var droptarget = document.getElementById("droptarget"); function handleEvent(event){ var info = "", output = document.getElementById("output"), data, xhr, files, i, len; EventUtil.preventDefault(event); if (event.type == "drop"){ data = new FormData(); files = event.dataTransfer.files; i = 0; len = files.length; while (i < len){ data.append("file" + i, files[i]); i++; } xhr = new XMLHttpRequest(); xhr.open("post", "FileAPIExample06Upload.php", true); xhr.onreadystatechange = function(){ if (xhr.readyState == 4){ alert(xhr.responseText); } }; xhr.send(data); } } EventUtil.addHandler(droptarget, "dragenter", handleEvent); EventUtil.addHandler(droptarget, "dragover", handleEvent); EventUtil.addHandler(droptarget, "drop", handleEvent);
只有Worker接收到消息纔會實際執行文件彙總的代碼。要給Worker傳遞消息,能夠使用postMessage()方法,消息內容能夠是任何可以被序列化的值
worker.postMessage(「start! ");
Web Worker自己也是一個最小化的運行環境
Worker的全局做用域提供這個功能,能夠調用importScripts()方法,這個方法接收一個或多個指向JavaScript文件的URL,每一個加載過程都是異步進行的。所以全部腳本加載以後,importScripts()纔會執行
//Web Worker 內部的代碼 importScripts("file1.js", "file2.js");