-
-
Save stdclass/1175365 to your computer and use it in GitHub Desktop.
var blinkify = function( el, // DOM-Element | |
rate // Speed (Default: 500 ms) | |
){ | |
setInterval(function(){ // Interval | |
el.style.opacity ^= 1 // change the visibility | |
}, rate || 500 ); |
function(a,b){setInterval(function(){a.style.opacity^=1},b||500)} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
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": "blinkify", | |
"description": "The Comeback of the Blink-Tag", | |
"keywords": [ | |
"blink", | |
"blinktag", | |
"retro" | |
] | |
} |
<!doctype html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<b id="test">BLINKY</b><br /> | |
<b id="test2">BLINKY - fast</b> | |
<script> | |
var blinkify = function( el, rate ){ | |
setInterval(function(){ | |
el.style.opacity ^= 1 | |
}, rate || 500 ); | |
} | |
blinkify( document.getElementById( "test" ) ); | |
blinkify( document.getElementById( "test2" ), 100 ); | |
</script> | |
</body> | |
</html> |
It seems that ^= is also a good stuff.
function(a,b,c){setInterval(function(){a.style.visibility=(c^=1)?"hidden":"visible"},b||5e2)}
Why not use c=!c
?
@michaelficarra: You are right! Thanks for reminding this. Since I am playing bitwise these days.
also, you're not saving much space by using 5e2
over 500
!
We can reuse the delay
argument and save 2 bytes:
function(a,b){setInterval(function(){a.style.visibility=(b=!b)?"hidden":"visible"},b||500)}
Would you mind either adapting or removing the README.md? Thank you! Additionally, you could omit the "visible" (because it is set by default anyway):
function(a,b){setInterval(function(){a.style.visibility=(b=!b)?"hidden":""},b||500)}
Normally I would say keep the "visible" for there might very well be a CSS rule setting it to "hidden", but this is 140byt.es so...
atk I don't know if this is a typo, but you used a BINARY OR in the delay instead of the LOGICAL OR. This will mess up most delays: the |
and ``||` only rarely have the same result.
Just a typo, fixed. Update: thanks, @phillipdornauer
Thanks to @atk we can go further and exploit the falsiness of empty string:
function(a,b){setInterval(function(){a.style.visibility=b=!b?"hidden":""},b||500)}
(82 bytes)
reordered to save the !
:
function(a,b){setInterval(function(){b=a.style.visibility=b?"":"hidden"},b||500)}
(81 bytes)
Subzey, jed: that was one sweet move.
Sacrificing a little more backward compatibility we can reach 70 bytes:
function(a,b){setInterval(function(){b=a.style.opacity=b?0:1},b||500)}
well, if you're gonna go down that road...
function(a,b){setInterval(function(){b=a.style.opacity=+!b},b||500)}
(68 bytes)
or for that matter:
function d(a,b,c){c=a.style.opacity=+!c;setTimeout(d,b||500,a,b,c)}
(67 bytes)
or even:
function d(a,b,c){setTimeout(d,b||500,a,b,a.style.opacity=+!c)}
(63 bytes, assuming you're okay with losing old browsers)
Interesting.
Supporting the current and previous version of each major browser would be nice. No ?
IE8, does not support setTimeout(function,delay,arg1,arg2,...)
, so... 68 ?
i'm happy with 68 (unless we want to dial down the default to 99
for 67).
Oh dear, it's amazing.
And finally i make bitwise trick useful.
function(a,b){setInterval(function(){a.style.opacity^=1},b||500)}
(65 bytes)
whoa, nice! i think that's the first useful XOR assignment i've ever seen here.
I think that's it. There's not much code left to optimize here.
Sorry for the b=...=b?0:1
in my previous comment. That was lazy. It was obvious there was room for improvements.
You can use xor to do toggle.
function(a,b,c){setInterval(function(){a.style.visibility=(c=c^1)?"hidden":"visible"},b||5e2)}