在标记传单地图的弹出窗口中插入多个倒计时
Insert multiple countdown on the popup of a marker leaflet map
我的目标是在每个弹出标记上显示基于 Json (obj.time) 的倒计时。
倒计时必须显示在 <div id='demo'>
如果我为所有 <div id='demo'>
插入一个倒计时,但没有多个值。
我尝试将 obj[i].id 连接到 id = 'demo' 以便每个倒计时都有多个 id
像这样 <div id='demo"+obj[i].id+"'>
然后我对
document.getElementById("demo"+obj[i].id).innerHTML = hours + "h "+ minutes + "m " + seconds + "s ";
我收到的错误是 Uncaught TypeError: Cannot read property 'id' of undefined at (index):215
你必须在区间函数中加入一个新的循环:
var x = setInterval(function() {
for(var i = 0; i < obj.length; i++){
if(document.getElementById("demo"+obj[i].id)){
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = new Date(obj[i].time).getTime() - now;
// If the count down is finished, write some text and continue loop
if (distance < 0) {
document.getElementById("demo"+obj[i].id).innerHTML = "EXPIRED";
continue;
}
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo"+obj[i].id).innerHTML = hours + "h "+ minutes + "m " + seconds + "s ";
}
}
}, 200);
您还可以将间隔设置得更快,如果您打开弹出窗口,它会更快地显示计时器。
我的目标是在每个弹出标记上显示基于 Json (obj.time) 的倒计时。
倒计时必须显示在 <div id='demo'>
如果我为所有 <div id='demo'>
插入一个倒计时,但没有多个值。
我尝试将 obj[i].id 连接到 id = 'demo' 以便每个倒计时都有多个 id
像这样 <div id='demo"+obj[i].id+"'>
然后我对
document.getElementById("demo"+obj[i].id).innerHTML = hours + "h "+ minutes + "m " + seconds + "s ";
我收到的错误是 Uncaught TypeError: Cannot read property 'id' of undefined at (index):215
你必须在区间函数中加入一个新的循环:
var x = setInterval(function() {
for(var i = 0; i < obj.length; i++){
if(document.getElementById("demo"+obj[i].id)){
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = new Date(obj[i].time).getTime() - now;
// If the count down is finished, write some text and continue loop
if (distance < 0) {
document.getElementById("demo"+obj[i].id).innerHTML = "EXPIRED";
continue;
}
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo"+obj[i].id).innerHTML = hours + "h "+ minutes + "m " + seconds + "s ";
}
}
}, 200);
您还可以将间隔设置得更快,如果您打开弹出窗口,它会更快地显示计时器。