window.onload dom tree, img, videos ready
document.onload dom tree ready
$('document').ready() dom tree ready
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wsConnection = new WebSocket('ws://localhost:3000/websocket'); | |
wsConnection.onopen = () => { | |
console.log('websocket connection is opened') | |
wsConnection.send('A'); | |
}; | |
wsConnection.onmessage = event => { | |
if (event.data === '9') { | |
setTimeout(() => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example below is using express and express-ws | |
app.ws('/websocket', (ws, req) => { | |
ws.on('message', (msg) => { | |
if (msg === 'A') { | |
ws.send('9'); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example below uses express.js | |
let counter = 0; | |
app.get('/longpolling', (req, res, next) => { | |
const delay = Math.random(); | |
setTimeout(() => { | |
res.send({ | |
random: delay, | |
sequence: counter | |
}); | |
counter++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fetchUrl } from 'fetch'; | |
const registerLongPolling = () => { | |
let counter = 0; | |
fetchUrl('http://localhost:3000/longpolling', (error, meta, body) => { | |
if (error) { | |
return; | |
} | |
if (counter > 4) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* On the server side, when get the request from eventSource, | |
* response with content type as 'text/event-stream', | |
* and write data in the format according to | |
* https://www.w3.org/TR/2009/WD-eventsource-20090421/ | |
*/ | |
const constructSSEMsg = (res, id, data) => { | |
res.write('id: ' + id + '\n'); | |
res.write('data: ' + data + '\n\n'); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const evtSource = new EventSource('/sse'); | |
evtSource.onmessage = event => { | |
try { | |
const jsonData = JSON.parse(event.data); | |
console.log(jsonData); | |
} catch (e) { | |
console.log(e); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const throttle = (func, delay) => { | |
let executable = true; | |
return () => { | |
if (executable) { | |
func(); | |
executable = false; | |
setTimeout(() => { | |
executable = true; | |
}, delay); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function functionsProcessor(funcArr, initValue, ...args) { | |
if(!funcArr.length) return initValue | |
var cur = funcArr.shift() | |
return functionsProcessor(funcArr, cur(initValue, ...args), ...args) | |
} | |
function functionsProcessor(funcArr, initValue, ...args) { | |
for(var i = 0; i < funcArr.length; i++){ | |
initValue = funcArr[i](initValue, ...args) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
function deepEqual(obj1, obj2){ | |
var keys1 = Object.keys(obj1) | |
var keys2 = Object.keys(obj2) | |
if(keys1.length !== keys2.length) return false | |
for(var key in obj1){ |
NewerOlder