Skip to content

Instantly share code, notes, and snippets.

@defims
Forked from atk/LICENSE.txt
Created June 13, 2016 11:24
Show Gist options
  • Select an option

  • Save defims/425d5af6bf76c7d353ff9344634f64af to your computer and use it in GitHub Desktop.

Select an option

Save defims/425d5af6bf76c7d353ff9344634f64af to your computer and use it in GitHub Desktop.
elementready in 94 bytes
function(
i, // id of element which needs to be selectable until callback can be executed
c, // callback to be executed in the elements scope
t // placeholder for timed out function
){
// create function t
(t = function(e){
// try to select node by ID (i)
(e = document.getElementById(i)) ?
// if we get an result, call callback in its scope
c.call(e) :
// otherwise let it set itself with a 45ms timeout
setTimeout(t,45)
// and execute itself immediately
})()
}
export function r(i,c,t){(t=function(e){(e=document.getElementById(i))?c.call(e):setTimeout(t,45)})()}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alex Kloss <alexhtkloss@web.de>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "elementready",
"description": "Waits until an element selectable by ID is available and then runs a callback in the elements scope.",
"keywords": [ "element", "ready" ],
"version": "0.1.0"
}
<!DOCTYPE html>
<title>Element Ready</title>
<div>Expected value: <b>works</b></div>
<script>
var er = function (i,c,t){(t=function(e){(e=document.getElementById(i))?c.call(e):setTimeout(t,45)})()};
// b#ret is not yet available, so lets wait for it.
er('ret', function(){ this.innerHTML = 'works' });
</script>
<div>Actual value: <b id="ret"></b></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment