Skip to content

Instantly share code, notes, and snippets.

@fragje
Created March 7, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fragje/01f10fc8bc5d6dfcb364 to your computer and use it in GitHub Desktop.
Save fragje/01f10fc8bc5d6dfcb364 to your computer and use it in GitHub Desktop.
JS callback examples
// 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