Skip to content

Instantly share code, notes, and snippets.

@greyaperez
Created May 28, 2014 18:53
Show Gist options
  • Save greyaperez/1339d27d11da6777abc7 to your computer and use it in GitHub Desktop.
Save greyaperez/1339d27d11da6777abc7 to your computer and use it in GitHub Desktop.
Auto Reload CSS
/*
* Usage (2 Choices):
* Option 1 - Auto Reload on Interval
* -- autoCSS.start()
*
* Option 2 - Reload on Demand
* -- autoCSS.init()
* -- autoCSS.refresh()
*/
var autoCSS = {
sheets : [],
urls : [],
interval : 2000,
process : false,
init : function(){
if(autoCSS.sheets.length === 0){
autoCSS.sheets = [].slice.call(document.getElementsByTagName('link'), 0).filter(function(el){ return (el.getAttribute('rel') === 'stylesheet'); });
}
for(i=0; i < autoCSS.sheets.length; i++){
autoCSS.urls.push( autoCSS.sheets[i].getAttribute('href') );
}
},
include : function(inc){
autoCSS.sheets.push(inc);
},
start : function(){
autoCSS.init();
autoCSS.process = setInterval(autoCSS.refresh,this.interval);
},
stop : function(){
clearInterval(autoCSS.process);
},
refresh : function(){
for(i=0; i < autoCSS.sheets.length; i++){
autoCSS.sheets[i].setAttribute('href',autoCSS.urls[i]+'?s='+new Date / 1000);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment