I'm trying to create a counter, that updates every 1 second. I've made a backend function that returns every 30 seconds, which is called with Ajax. The result from the call is divided by 30 and should then update the counter every 1 second for 30 seconds. How would I go about putting a sleep in the for-loop?
This is my code so far:
function getCount() {
$.ajax({
url: '@Url.Action("", "", new {area = ""})',
type: 'POST',
success: function (data) {
var newTotalCount = data.totalCount;
var newDanishCount = data.danishCount;
var newNorwayCount = data.norwayCount;
var newSwedenCount = data.swedenCount;
var newUsCount = data.usCount;
var currentTotalCount = $("#odoTotal").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
var currentDanishCount = $("#odoDk").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
var currentNorwayCount = $("#odoNo").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
var currentSwedenCount = $("#odoSe").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
var currentUsCount = $("#odoUs").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
var updateTotalCount = newTotalCount - currentTotalCount;
var updateDanishCount = newDanishCount - currentDanishCount;
var updateNorwayCount = newNorwayCount - currentNorwayCount;
var updateSwedenCount = newSwedenCount - currentSwedenCount;
var updateUsCount = newUsCount - currentUsCount;
var updateTotalPerSecond = updateTotalCount / 30;
var updateDanishPerSecond = updateDanishCount / 30;
var updateNorwayPerSecond = updateNorwayCount / 30;
var updateSwedenPerSecond = updateSwedenCount / 30;
var updateUsPerSecond = updateUsCount / 30;
getAllSales();
for (var i = 0; i < 30; i++) {
window.setTimeout(function() {
$("#odoTotal").html(currentTotalCount+updateTotalPerSecond);
$("#odoDk").html(currentDanishCount+updateDanishPerSecond);
$("#odoNo").html(currentNorwayCount+updateNorwayPerSecond);
$("#odoSe").html(currentSwedenCount+updateSwedenPerSecond);
$("#odoUs").html(currentUsCount+updateUsPerSecond);
currentTotalCount = $("#odoTotal").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
currentDanishCount = $("#odoDk").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
currentNorwayCount = $("#odoNo").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
currentSwedenCount = $("#odoSe").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
currentUsCount = $("#odoUs").text().replace(/,/g, "").replace(/(\r\n|\n|\r)/gm, "");
}, 1000);
}
}
});
}