Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Faster and neater way to do this?

    Does anyone know a faster and less repetative way to do this? It is for whether effects on a game.

    Here is my code:
    Code:
    function Night(css) {
    	if (hora >= 21 && hora <= 24 || hora >= 0 && hora <= 5) {
    		var head, style;
    		head = document.getElementsByTagName('head')[0];
    		if (!head) { return; }
    		style = document.createElement('style');
    		style.type = 'text/css';
    		style.innerHTML = css;
    		head.appendChild(style);
    	};
    };
    
    function Sunrise(css) {
    	if ( hora >= 6 && hora <=  9 ) {
    		var head, style;
    		head = document.getElementsByTagName('head')[0];
    		if (!head) { return; }
    		style = document.createElement('style');
    		style.type = 'text/css';
    		style.innerHTML = css;
    		head.appendChild(style);
    	};
    };
    
    function Sunset(css) {
    	if ( hora >= 18 && hora <= 20 ) {
     	   var head, style;
     	   head = document.getElementsByTagName('head')[0];
     	   if (!head) { return; }
     	   style = document.createElement('style');
     	   style.type = 'text/css';
     	   style.innerHTML = css;
     	   head.appendChild(style);
    	}; 
    };
    
    function Rain(css) {
    	if ( hora >= rainstarttime && hora <= rainendtime ) {
        	var head, style;
    	    head = document.getElementsByTagName('head')[0];
        	if (!head) { return; }
    	    style = document.createElement('style');
    	    style.type = 'text/css';
    	    style.innerHTML = css;
        	head.appendChild(style);
    	};
    };
    
    function Fog(css) {
    	if ( hora >= fogstarttime && hora <= fogendtime) {
    		var head, style;
    	    head = document.getElementsByTagName('head')[0];
    	    if (!head) { return; }
    	    style = document.createElement('style');
    	    style.type = 'text/css';
    	    style.innerHTML = css;
    	    head.appendChild(style);
    	};
    };
    an example bit of the function being used is:
    Code:
    Night('#city #container .phase2, #city #container .phase3 {    background-image:url('+webadress+'Night/city_phase2.jpg);}');
    thanks

  • #2
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Since the CSS the function is using is being supplied by you, why not just have one function?
    Code:
    function setWeather(css)
    {
    	var head, style;
    	head = document.getElementsByTagName('head')[0];
    	if (!head) {return;}
    
    	style = document.createElement('style');
    	style.type = 'text/css';
    	style.innerHTML = css;
    
    	head.appendChild(style);
    };
    Also why are you doing time checks inside each function? Why not check the time outside then decide which you want to call?
    Code:
    if (hora >= 21 && hora <= 24 || hora >= 0 && hora <= 5)
    {
    	Night(/* Night CSS */);
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •