...

View Full Version : Faster and neater way to do this?



Pezmc
06-08-2008, 10:15 PM
Does anyone know a faster and less repetative way to do this? It is for whether effects on a game.

Here is my 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:

Night('#city #container .phase2, #city #container .phase3 { background-image:url('+webadress+'Night/city_phase2.jpg);}');

thanks

mr e
06-09-2008, 05:01 AM
Since the CSS the function is using is being supplied by you, why not just have one function?


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?


if (hora >= 21 && hora <= 24 || hora >= 0 && hora <= 5)
{
Night(/* Night CSS */);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum