Created
July 31, 2018 14:11
-
-
Save mborodov/24e5f2a04b8da2c739b06d6eb7c77050 to your computer and use it in GitHub Desktop.
Exec JS function for deffered DOM elements with window.requestAnimationFrame
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<script src="https://code.jquery.com/jquery-1.9.1.js"></script> | |
</head> | |
<body> | |
<p> | |
For check open console and type $("<div>").appendTo("body"); | |
</p> | |
<script> | |
var batDefferedElements = [ | |
{ selector: 'div', show: true } | |
]; | |
var batDefferedCallback = function() { | |
console.log(123); | |
}; | |
var batDefferedElementsReady = function () { | |
var elementsError = typeof batDefferedElements == 'undefined'; | |
var callbackError = typeof batDefferedCallback == 'undefined' || typeof batDefferedCallback != 'function'; | |
if (elementsError || callbackError) { | |
if (elementsError) | |
console.warn('Deffered elements not defined in global variable batDefferedElements'); | |
if (callbackError) | |
console.warn('Deffered callback batDefferedCallback not defined or type not function'); | |
return; | |
} | |
// If all elements exists | |
var allShowed = false; | |
batDefferedElements.forEach(function(element) { | |
allShowed = element.show ? $(element.selector).size() : !$(element.selector).size(); | |
}); | |
if (allShowed) { | |
batDefferedCallback(); | |
return; | |
} | |
console.info('search...'); | |
window.requestAnimationFrame(batDefferedElementsReady); | |
}; | |
// Initialize our ready() function | |
window.requestAnimationFrame(batDefferedElementsReady); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment