2011-12-25 16 views
3

クリスマスにのみ「雪」にしようとするのに少し問題があります。私はその日をつかむ方法を考え出しましたが、SnowStorm()を正しく呼び出す方法がわかりません。クリスマスに関数を呼び出す

あなたが isItChristmas関数からブール値を返す必要が
var christmas = { 
    month: 11, 
    date: 25 
} 

function isItChristmas() { 
    var now = new Date(); 
    var isChristmas = (now.getMonth() == christmas.month && now.getDate() == christmas.date); 

if (isChristmas) 
    return ; 
    else 
    return ; 
} 

var snowStorm = null; 

function SnowStorm() { 
    (snowstorm code) 

} 

snowStorm = new SnowStorm(); 
+1

あなたは具体的にどっちつかんでいますか? 'isItChristmas'の部分ですか?あなたの 'snowStorm()'関数を空の 'if()'ステートメントに入れる方法は?他に何か? – Bojangles

答えて

1

を追加 第二には、ここでバージョンです:

var christmas = { 
    month: 11, 
    date: 25 
} 

function isItChristmas() { 
    var now = new Date(); 
    return (now.getMonth() == christmas.month && now.getDate() == christmas.date); 
} 

if (isItChristmas()){ 
    SnowStorm(); 
    }else{ 
    //not a christmas 
} 

function SnowStorm() { 
    (snowstorm code) 
} 

あなたが作ることを計画している場合はここに1つですクラス:

var christmas = { 
    month: 11, 
    date: 25 
} 

function isItChristmas() { 
    var now = new Date(); 
    return (now.getMonth() == christmas.month && now.getDate() == christmas.date); 
} 

var storm = new SnowStorm(); 

if (isItChristmas()){ 
    storm.Snow(); 
    }else{ 
    //not a christmas 
} 

function SnowStorm() { 
    this.Snow = function(){ 
     (snowstorm code) 
    } 
} 
0

:あなたは.getMonth()メソッドが返すことがわかりますところで

function SnowStorm() { 
    if (isItChristmas()) { 
     // your snowstorm code here 
     // that will execute only on Christmas 
    } 
} 

:その後、

function isItChristmas() { 
    var now = new Date(); 
    return (now.getMonth() + 1 == christmas.month && 
      now.getDate() == christmas.date); 
} 

とそれを呼び出します月は0から11までですので、私の例に示すように1を追加する必要があります。そうでなければ、あなたのコードは11月25日に実行されます。

0

まず、isItChristmas関数では何も返されません。あなたが機能(ないクラス)を作ることを計画している場合は吹雪機能でちょうど

if (isItChristmas) { 
    (your code here) 
}