更改函数以使用参数而不是硬编码值

时间:2013-07-07 23:45:49

标签: javascript function

我正在使用以下代码在我的页面上创建一个倒数计时器,并且除了将所有日期信息硬编码到“getSeconds”函数中并且我想要这样做以便我可以在同一页面上有多个倒计时,我猜这意味着“getSeconds”函数必须更改为接受参数,但我不太清楚如何做到这一点,并认为我会请专家来帮助他们。

<html>
  <head>

    <script type = "text/javascript">

var cday;
var timeInSecs;
var ticker;

function getSeconds() {
    var now = new Date();
    var nowtime = now.getTime(); // time now in milliseconds
    var countdowntime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0); //  16 hrs = 4 pm
    // countdowntime - change time hh,mm,ss to whatever time required, e.g. 7,50,0 (0750)
    var dy = 0; // Friday (day 5) - change for other days 0-6
    var atime = countdowntime.getTime();
    var diff = parseInt((atime - nowtime) / 1000); // positive if date is in future
    if (diff > 0) {
        cday = dy - now.getDay();
    } else {
        cday = dy - now.getDay() - 1;
    }
    if (cday < 0) {
        cday += 7;
    } // aleady passed countdown time, so go for next week
    if (diff <= 0) {
        diff += (86400 * 7)
    }
    startTimer(diff);
}

function startTimer(secs) {
    timeInSecs = parseInt(secs);
    ticker = setInterval("tick()", 1000);
    tick(); // to start counter display right away
}

function tick() {
    var secs = timeInSecs;
    if (secs > 0) {
        timeInSecs--;
    } else {
        clearInterval(ticker); // stop counting at zero
        getSeconds(); // and start all over again! 
    }
    var days = Math.floor(secs / 86400);
    secs %= 86400;
    var hours = Math.floor(secs / 3600);
    secs %= 3600;
    var mins = Math.floor(secs / 60);
    secs %= 60;
    var result = "Time remaining " + cday + ' day(s) ';
    result += ((hours < 10) ? "0" : "") + hours + " hours " + ((mins < 10) ? "0" : "") + mins + " minutes " + ((secs < 10) ? "0" : "") + secs + " seconds";
    document.getElementById("countdown").innerHTML = result;
}
    </script>
  </head>

  <body onload = "getSeconds()">
    <span id="countdown" style="font-size: 20; font-weight:bold;"> </span>
  </body>
</html>

3 个答案:

答案 0 :(得分:0)

很简单。一个例子:

function myFunction(var1,var2) {
//some code
}

如此简单地更改那些变量的日期信息。 如果要调用函数,请将其与之前编写的参数一起使用。

修改

你可以这样做,分别传递每个参数:

function getSeconds(hours, minutes, seconds, daycode) {
    var now = new Date();
    var nowtime = now.getTime(); // time now in milliseconds
    var countdowntime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, seconds); //  16 hrs = 4 pm
    // countdowntime - change time hh,mm,ss to whatever time required, e.g. 7,50,0 (0750)
    var dy = daycode; // Friday (day 5) - change for other days 0-6
    var atime = countdowntime.getTime();
    var diff = parseInt((atime - nowtime) / 1000); // positive if date is in future
    if (diff > 0) {
        cday = dy - now.getDay();
    } else {
        cday = dy - now.getDay() - 1;
    }
    if (cday < 0) {
        cday += 7;
    } // aleady passed countdown time, so go for next week
    if (diff <= 0) {
        diff += (86400 * 7)
    }
    startTimer(diff);
}

您还可以传递一个对象参数,例如:

countdown = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0); //  16 hrs = 4 pm

function getSeconds(countdown, daycode) {
    var now = new Date();
    var nowtime = now.getTime(); // time now in milliseconds
    var countdowntime = countdown
    ...

答案 1 :(得分:0)

以下是将年,月和日期作为参数传递的示例,并用于倒数计时。当你调用getSeconds函数时,只需传入getSeconds(2013,7,8)

function getSeconds(varYear, varMonth, varDate) {
    var now = new Date();
    var nowtime = now.getTime(); // time now in milliseconds
    var countdowntime = new Date(varYear, varMonth, varDate, 0, 0, 0); //  16 hrs = 4 pm
    // countdowntime - change time hh,mm,ss to whatever time required, e.g. 7,50,0 (0750)
    var dy = 0; // Friday (day 5) - change for other days 0-6
    var atime = countdowntime.getTime();
    var diff = parseInt((atime - nowtime) / 1000); // positive if date is in future
    if (diff > 0) {
        cday = dy - now.getDay();
    } else {
        cday = dy - now.getDay() - 1;
    }
    if (cday < 0) {
        cday += 7;
    } // aleady passed countdown time, so go for next week
    if (diff <= 0) {
        diff += (86400 * 7)
    }
    startTimer(diff);
    }

答案 2 :(得分:0)

看起来变量countdowntime是您需要作为参数传递给函数getSeconds()以便具有多个倒计时的变量。您只需要更改函数定义以将Date对象作为参数接受,如下所示:

function getSeconds(countdowntime){

并注释掉当前声明countdowntime对象的行。然而,我注意到的其他一点是你使用全局变量在函数之间共享数据。这是你可能需要考虑重做的事情,因为现在会有多个计时器。