Skip to content

Instantly share code, notes, and snippets.

View chrisbuttery's full-sized avatar
🏠
Working from home

Chris Buttery chrisbuttery

🏠
Working from home
View GitHub Profile
@chrisbuttery
chrisbuttery / 1.js
Last active April 6, 2024 16:10
Fade in / Fade out
// fade out
function fade(el) {
var op = 1;
var timer = setInterval(function () {
if (op <= 0.1){
clearInterval(timer);
el.style.display = 'none';
}
el.style.opacity = op;
@chrisbuttery
chrisbuttery / tweet1.json
Last active August 29, 2015 14:05
Example tweets
{
"user": {
"name": "Lonely Bob",
"handle": "lonelyBob"
},
"message": "Oh man, did you just hear that? What was that?!!!",
"favourited": true
}
@chrisbuttery
chrisbuttery / promises.html
Created August 15, 2014 10:45
Synchronous asynchronous JavaScript - Promises
<html>
<head>
<style>
body {
background: #f5f8fa;
font: 16px/22px Arial, sans-serif;
margin: 0;
}
.statList {
background: #fff;
@chrisbuttery
chrisbuttery / callback-hell.html
Created August 15, 2014 10:46
Synchronous asynchronous JavaScript - Callback hell
<html>
<head>
<style>
body {
background: #f5f8fa;
font: 16px/22px Arial, sans-serif;
margin: 0;
}
.statList {
background: #fff;
@chrisbuttery
chrisbuttery / slightly-nicer-callbacks.html
Created August 15, 2014 10:47
Synchronous asynchronous JavaScript - Slightly nicer callbacks
<html>
<head>
<style>
body {
background: #f5f8fa;
font: 16px/22px Arial, sans-serif;
margin: 0;
}
.statList {
background: #fff;
@chrisbuttery
chrisbuttery / nicer-callbacks.html
Created August 15, 2014 10:47
Synchronous asynchronous JavaScript - Nicer callbacks
<html>
<head>
<style>
body {
background: #f5f8fa;
font: 16px/22px Arial, sans-serif;
margin: 0;
}
.statList {
background: #fff;
{
"users": [
{
"firstName": "Max",
"lastName": "Wheeler",
"username": "maxwheeler",
"checkedIn": true
},
{
"firstName": "Tim",
function hostReachable() {
// Handle IE and more capable browsers
var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );
var status;
// Open new request as a HEAD to the root hostname with a random param to bust the cache
xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false );
// Issue request and handle response
@chrisbuttery
chrisbuttery / index.js
Created January 11, 2015 11:08
generatorz
function *write(){
console.log('writing');
var num = yield 1;
console.log('num x num =', num * num);
}
function *blah () {
console.log('starting blah');
yield* write();
console.log('ending blah');
@chrisbuttery
chrisbuttery / index.js
Last active March 1, 2018 05:19
Control the flow of asynchronous calls with ES6 generator functions
/**
* get - XHR Request
*/
let get = function (url) {
return function (callback) {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onreadystatechange = function() {
let response = xhr.responseText;