Skip to content

Instantly share code, notes, and snippets.

@stdclass
Forked from 140bytes/LICENSE.txt
Created August 27, 2011 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stdclass/1175365 to your computer and use it in GitHub Desktop.
Save stdclass/1175365 to your computer and use it in GitHub Desktop.
The Comeback of the BLINK-Tag
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>
@tsaniel
Copy link

tsaniel commented Aug 27, 2011

You can use xor to do toggle.

function(a,b,c){setInterval(function(){a.style.visibility=(c=c^1)?"hidden":"visible"},b||5e2)}

@tsaniel
Copy link

tsaniel commented Aug 27, 2011

It seems that ^= is also a good stuff.

function(a,b,c){setInterval(function(){a.style.visibility=(c^=1)?"hidden":"visible"},b||5e2)}

@michaelficarra
Copy link

Why not use c=!c?

@tsaniel
Copy link

tsaniel commented Aug 28, 2011

@michaelficarra: You are right! Thanks for reminding this. Since I am playing bitwise these days.

@jed
Copy link

jed commented Aug 28, 2011

also, you're not saving much space by using 5e2 over 500!

@p01
Copy link

p01 commented Aug 28, 2011

We can reuse the delay argument and save 2 bytes:
function(a,b){setInterval(function(){a.style.visibility=(b=!b)?"hidden":"visible"},b||500)}

@atk
Copy link

atk commented Aug 29, 2011

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)}

@p01
Copy link

p01 commented Aug 29, 2011

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.

@atk
Copy link

atk commented Aug 29, 2011

Just a typo, fixed. Update: thanks, @phillipdornauer

@subzey
Copy link

subzey commented Aug 30, 2011

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)

@jed
Copy link

jed commented Aug 30, 2011

reordered to save the !:

function(a,b){setInterval(function(){b=a.style.visibility=b?"":"hidden"},b||500)}

(81 bytes)

@p01
Copy link

p01 commented Aug 30, 2011

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)}

@jed
Copy link

jed commented Aug 30, 2011

well, if you're gonna go down that road...

function(a,b){setInterval(function(){b=a.style.opacity=+!b},b||500)}

(68 bytes)

@jed
Copy link

jed commented Aug 30, 2011

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)

@p01
Copy link

p01 commented Aug 30, 2011

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 ?

@jed
Copy link

jed commented Aug 30, 2011

i'm happy with 68 (unless we want to dial down the default to 99 for 67).

@tsaniel
Copy link

tsaniel commented Aug 30, 2011

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)

@jed
Copy link

jed commented Aug 30, 2011

whoa, nice! i think that's the first useful XOR assignment i've ever seen here.

@p01
Copy link

p01 commented Aug 30, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment