在html javascript中设置jquery参数

在html javascript中设置jquery参数,jquery,timer,countdown,Jquery,Timer,Countdown,我正在使用js文件计算来自数据库的元素的剩余时间。每个元素都有一个过期时间。通过计算,我想向最终用户显示剩余时间。就这样我遇到了一个问题。所有的倒计时计时器都显示相同的时间,请帮我解决它。 js文件 函数倒计时(){ this.start_time=“02:00:00:23”; this.target_id=“.timer”; this.name=“timer”; } CountDown.prototype.init=function(){ 这是reset(); setInterval(this

我正在使用js文件计算来自数据库的元素的剩余时间。每个元素都有一个过期时间。通过计算,我想向最终用户显示剩余时间。就这样我遇到了一个问题。所有的倒计时计时器都显示相同的时间,请帮我解决它。 js文件

函数倒计时(){
this.start_time=“02:00:00:23”;
this.target_id=“.timer”;
this.name=“timer”;
}
CountDown.prototype.init=function(){
这是reset();
setInterval(this.name+'.tick()',1000);
}
CountDown.prototype.reset=函数(){
时间=此.start\u time.split(“:”);
this.days=parseInt(时间[0]);
this.hours=parseInt(时间[1]);
this.minutes=parseInt(时间[2]);
this.seconds=parseInt(时间[3]);
这个。更新_target();
}
CountDown.prototype.tick=function(){
如果(this.seconds>0 | | | this.minutes>0 | | | this.hours>0 | | this.days>0){
如果(this.hours==0&&this.minutes==0&&this.seconds==0){
this.days=this.days-1;
这个小时=23;
这1.5分钟=59;
这个秒=59;
}
如果(this.minutes==0&&this.seconds==0){
this.hours=this.hours-1;
这1.5分钟=59;
这个秒=59;
}
else if(this.seconds==0){
this.minutes=this.minutes-1;
这个秒=59;
}
否则{
this.seconds=this.seconds-1;
}
}
这个。更新_target();
}
CountDown.prototype.update_target=函数(){
秒=这个。秒;
分钟=this.minutes;
小时=这个。小时;
天=这个。天;

如果(秒,因为您使用类名作为元素选择器。请改用ID

function CountDown(element_id) {
    this.start_time = "02:00:00:23";
    this.target_id = "#" + element_id;
    this.name = "timer";
}
我会创建这样的对象:

<input type="text" id="@item.ID" class="timer" style="height:30px;color:black"/>
<script>
    timer[@i] = new CountDown('@item.ID');
    timer[@i].start_time = "@item.name";
    timer[@i].init();
</script>
$(this.target_id).val(days+":"+hours+":"+minutes + ":" + seconds);
CountDown.prototype.init=function(){
    this.reset();
    var timer = this;
    setInterval(function () { timer.tick() },1000);
}

.ini()
替换为以下内容:

<input type="text" id="@item.ID" class="timer" style="height:30px;color:black"/>
<script>
    timer[@i] = new CountDown('@item.ID');
    timer[@i].start_time = "@item.name";
    timer[@i].init();
</script>
$(this.target_id).val(days+":"+hours+":"+minutes + ":" + seconds);
CountDown.prototype.init=function(){
    this.reset();
    var timer = this;
    setInterval(function () { timer.tick() },1000);
}

没有
timer.tick()
,此行
setInterval(this.name+'.tick()',1000)
在您的init原型中是不正确的。

在执行此版本时,我在jquery文件Uncaught TypeError中遇到以下错误:timer.tick不是一个函数thanxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx不客气,希望它能帮助您。如果您发现此答案有帮助,您可能希望将其标记为回答这个问题,这样我们也可以帮助像我们这样的人。