I want to make a timer start counting down right after receiving a javascript response

تبليغ
سؤال

يرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.

تبليغ
‎إلغاء

I want to make the timer count down just after receiving a quote from an api request. If user has not asked for the quote, the timer should keep set on the specified time (4 minutes in this case).

Here is the code I have so far for the timer:

function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);

minutes = minutes < 10 ? “0” + minutes : minutes;
seconds = seconds < 10 ? “0” + seconds : seconds;

display.textContent = minutes + “:” + seconds;

if (–timer < 0) {
timer = duration;
}
}, 1000);
}

window.onload = function () {
var fourMinutes = 60 * 4,
display = document.querySelector(‘#time’);
startTimer(fourMinutes, display);
};

<section>
<h6>Timer</h6><div><span id=”time”>04:00</span></div>
</section>

And this is the code for getting the quote:

async function getQuote() {
if (
!currentTrade.from ||
!currentTrade.to ||
!document.getElementById(“from”).value
)
return;

let amount = Number(
document.getElementById(“from”).value *
10 ** currentTrade.from.decimals
);

const quote = await Plug.quote({
network: “repl”,
fromAddress: currentTrade.from.address,
toAddress: currentTrade.to.address,
amount: amount,
});
console.log(quote);
document.getElementById(“to”).value =
quote.toAmount / 10 ** quote.to.decimals;
}

‫أضف إجابة

تصفح
تصفح

مجهول يجيب