Created
March 7, 2015 11:29
-
-
Save fragje/01f10fc8bc5d6dfcb364 to your computer and use it in GitHub Desktop.
JS callback examples
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
// Custom javascript | |
function fullName(firstName, lastName, callback) { | |
console.log('My name is ' + firstName + ' ' + lastName); | |
callback(lastName); | |
} | |
var greeting = function(ln) { | |
console.log("Welcome Mr. " + ln); | |
} | |
/// | |
window.watchResize = function(callback) { | |
var resizing; | |
function done() { | |
clearTimeout( resizing ); | |
resizing = null; | |
// Run callback after finish window resize | |
callback(); | |
} | |
window.onresize = function() { | |
if ( resizing ) { | |
clearTimeout( resizing ); | |
resizing = null; | |
} | |
resizing = setTimeout( done, 200 ); | |
} | |
// Run callback on pageload | |
callback(); | |
} | |
var browserWidth = 0; | |
window.watchResize(function() { | |
browserWidth = window.innerWidth || document.body.offsetWidth; | |
console.log('Browser width ' + browserWidth) | |
}); | |
// Create and append image element | |
var firstParagraph = document.getElementsByTagName('p')[0]; | |
var img = document.createElement('img'); | |
img.setAttribute('alt', 'Bilde'); | |
img.setAttribute('src', 'http://gfx.nrk.no/front/2015/02/27/c=395,287,1261,580;w=198;h=91;43500.jpg'); | |
firstParagraph.appendChild(img); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment