Skip to content

Instantly share code, notes, and snippets.

@Mao8a
Last active May 1, 2020 06:27
Show Gist options
  • Save Mao8a/6fb8a360a287cdfe44c7971cfd660cac to your computer and use it in GitHub Desktop.
Save Mao8a/6fb8a360a287cdfe44c7971cfd660cac to your computer and use it in GitHub Desktop.
JS: JavaScript Functions
// Immediately-Invoked Function Expression, or IIFE
(function () {
// your code...
})()
//Ready jQuery
$(function () {
// Code after jQuery is ready...
})
// Combined IIFE & jQuery
(function () {
$(function () {
// Code after jQuery is ready...
})
})()
//Minimized IIFE
!function(){/* Your Code here...*/}();
//ES6 IIFE
((foo) => {
// do something with foo here foo
})('foo value')
// # Observer
var
observe = function (obj, cb) {
function rp() {
var xobj = $(obj)
if (xobj.length && !xobj.hasClass('ready')) {
xobj.addClass('ready')
cb()
} else if (xobj.length && xobj.hasClass('ready')) {
} else {
setTimeout(function () {
rp()
}, 100)
}
}
rp()
},
observeAll = function (obj, cb) {
function rp() {
var xobj = $(obj)
if (xobj.length && !xobj.hasClass('ready')) {
xobj.addClass('ready')
cb()
setTimeout(function () {
rp()
}, 100)
} else {
setTimeout(function () {
rp()
}, 100)
}
}
rp()
},
observeInv = function (obj, cb) {
function rp() {
var xobj = $(obj)
if (!xobj.length) {
cb()
} else {
setTimeout(function () {
rp()
}, 100)
}
}
rp()
},
observeObj = function (dl, cb) {
function rp() {
xobj = window[dl]
if (xobj && xobj['object'] && !xobj.check) {
xobj.check = true
cb()
} else {
setTimeout(function () {
rp()
}, 100)
}
}
rp()
},
observeAllChilds = function (obj, cb) {
function rp() {
var xobj = $(obj)
if (xobj.length) {
xobj.each(function () {
if ($(this).length && !$(this).hasClass('ready')) {
$(this).addClass('ready')
cb($(this))
}
})
setTimeout(function () {
rp()
}, 100)
} else {
setTimeout(function () {
rp()
}, 100)
}
}
rp()
}
observeAll('selector', cb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment