Hi guys, I am making my project, a bidding website in asp.net core. I am trying to make logic to show the remaining time of auctions on the auction post like 03H 35M 50S remaining or something like that and the time will decrease second by second.
Also if the specific auction time has passed it's automatically removed from the page and the new one will appear.
What I have tried:
I have tried following to make a timer on my auction post:
<pre>$.ajax({
url: '@Url.Action("GetAllAuctions", "Home")',
dataType: 'json',
success: function (result) {
for (var i = 0; i < result.data.length; i++) {
var id = result.data[i].id;
var endDate = (result.data[i].endDate);
var startDate = (result.data[i].startDate);
var t = makeTimer(endDate, startDate);
$('#auctions').append('<div class="col-lg-4 col-md-6 col-sm-10"><div data-wow-duration="1.5s" data-wow-delay="0.2s" class="eg-card auction-card1 wow animate fadeInDown"><div class="auction-img"> <img alt="image" src=' + result.data[i].coverPhoto + '> <div class="auction-timer"><div class="countdown" id="timer"><h4>' + hrs + 'H : ' + mins + 'M : ' + secs + 'S</h4></div></div><div class="author-area"><div class="author-emo"><img alt="image" src="/assets/images/icons/smile-emo.svg"></div><div class="author-name"><span>by robatfox</span> </div></div></div> <div class="auction-content"> <h4><a href="@Url.Action("AuctionDetails", "Home")?id=' + id + '">' + result.data[i].title + '</a></h4> <p>Bidding Price : <span><span>' + result.data[i].price + '</span></span></p><div class="auction-card-bttm"><a href="@Url.Action("AuctionDetails", "Home")?id=' + id + '" class="eg-btn btn--primary btn--sm">Place a Bid</a> </div></div> </div></div>');
}
},
error: function (data) {
console.log(data);
}
})
This is the timer function:
<pre>function makeTimer(endtime, starttime) {
var endTime = new Date(endtime);
var endTime = (Date.parse(endTime)) / 1000;
var now = new Date(starttime);
var now = (Date.parse(now) / 1000);
var timeLeft = endTime - now;
var days = Math.floor(timeLeft / 86400);
var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600)) / 60);
var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));
if (hours < "10") {
hours = "0" + hours;
}
if (minutes < "10") {
minutes = "0" + minutes;
}
if (seconds < "10") {
seconds = "0" + seconds;
}
return {
days,
hours,
minutes,
seconds
};
}
setInterval(function () {
makeTimer();
}, 1000);
This is what i have tried but I am getting like on every post is 0hour 0minutes 0seconds only..