Javascript 将函数更改为使用参数而不是硬编码值

Javascript 将函数更改为使用参数而不是硬编码值,javascript,function,Javascript,Function,我正在使用下面的代码在我的页面上创建一个倒计时计时器,它工作得很好,只是所有的日期信息都硬编码到了“getSeconds”函数中,我想这样我就可以在同一页面上有多个倒计时,我猜这意味着“getSeconds”函数必须更改以接受参数,但我不太确定如何执行此操作,我想我会向这里的专家寻求帮助 <html> <head> <script type = "text/javascript"> var cday; var timeInSecs; var t

我正在使用下面的代码在我的页面上创建一个倒计时计时器,它工作得很好,只是所有的日期信息都硬编码到了“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>

var-cday;
时间昆虫;
var-ticker;
函数getSeconds(){
var now=新日期();
var nowtime=now.getTime();//以毫秒为单位的当前时间
var countdown=新日期(now.getFullYear(),now.getMonth(),now.getDate(),0,0,0);//16小时=下午4点
//countdowntime-将时间hh、mm、ss更改为所需的任何时间,例如7,50,0(0750)
var dy=0;//星期五(第5天)-更改为其他第0-6天
var atime=countdown.getTime();
var diff=parseInt((atime-nowtime)/1000);//如果日期在将来,则为正值
如果(差异>0){
cday=dy-now.getDay();
}否则{
cday=dy-now.getDay()-1;
}
如果(cday<0){
cday+=7;
}//阿莱迪已经过了倒计时时间,所以下周开始吧
如果(差异0){
时光虫;
}否则{
clearInterval(滴答声);//在零处停止计数
getSeconds();//然后重新开始!
}
变量天数=数学下限(秒/86400);
secs%=86400;
var小时=数学楼层(秒/3600);
秒%=3600;
var mins=数学层(秒/60);
secs%=60;
var result=“剩余时间”+cday+“天”;
结果+=((小时<10)?“0”:“”)+小时+“小时”+((分钟<10)?“0”:“”)+分钟+“分钟”+((秒<10)?“0”:“”)+秒+“秒”;
document.getElementById(“倒计时”).innerHTML=result;
}

非常简单。例如:

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);
}

下面是一个将年、月和日期作为参数传递的示例,用于计算停机时间。调用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);
    }
函数getSeconds(varYear、varMonth、varDate){ var now=新日期(); var nowtime=now.getTime();//以毫秒为单位的当前时间 var countdowntime=新日期(varYear、varMonth、varDate、0、0);//16小时=下午4点 //countdowntime-将时间hh、mm、ss更改为所需的任何时间,例如7,50,0(0750) var dy=0;//星期五(第5天)-更改为其他第0-6天 var atime=countdown.getTime(); var diff=parseInt((atime-nowtime)/1000);//如果日期在将来,则为正值 如果(差异>0){ cday=dy-now.getDay(); }否则{ cday=dy-now.getDay()-1; } 如果(cday<0){ cday+=7; }//阿莱迪已经过了倒计时时间,所以下周开始吧
如果(diff看起来变量
countdown
是需要作为参数传递给函数getSeconds()的变量,以便有多个倒计时。您只需更改函数定义,将该
Date
对象作为参数,如下所示:

function getSeconds(countdowntime){

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

您能将代码减少到我们可以使用的最小数量吗将看到问题?更改为仅显示中的函数question@JustinL.:没有问题,他只是想知道他需要更改什么。为此,我们需要完整的代码,因为它不仅包括
getSeconds
。我会理解,如果每个变量只有一个值,但如果您查看countdowntime变量,它包含一个数组,那么我该如何使用您的示例来实现它。我想我不会关注您,尽管您让它看起来很简单。我编辑了我的答案。如果您想在网页上放置倍数倒计时,只需调用您的函数传递这些倍数参数。在这种情况下,如果您有多个函数,您可以执行相同的过程,或者重写它们将参数从一个传递到另一个,这样您只能在一个函数中输入参数。谢谢,也许其他人可以使用它。但是我仍然无法理解它。我投了赞成票,因为您实际上花了时间对它进行编码和编辑。这不可能实现多次倒计时。应该改为传递一个对象。1参数,完全可以随时间扩展。@CalebC只处理部分问题,正如Bergi指出的,我仍然需要考虑小时、分钟、秒和星期几变量。@Derek我不了解你(我的JS知识非常有限)因此,在这方面,一个例子将是最有帮助的。他可能不仅需要考虑这一点,他还必须修正这些,以允许多次倒计时。谢谢,这正是我的意思:)谢谢大家的帮助。我想这不是我的脚本,我在JS方面的知识非常有限,似乎我已经打开了一个关于这个问题的漏洞。我想我只需要找到一个我可以使用的预先制定的解决方案。请投票选出最能回答问题的解决方案,因为我无法获得任何答案o按我的需要工作。
function getSeconds(countdowntime){