The following code divides 'invites' by 'accepts' and then rounds it to 2 decimal places, always keeping 2 decimal places even if the answer is 8.00
This is the code I am using:
function live_ratio()
{
// Check that the invites, accepts, and ratio spans have loaded
while(!document.getElementById("live_invites").innerHTML || !document.getElementById("live_accepts").innerHTML || !document.getElementById("live_ratio").innerHTML)
{
//alert('blah');
document.getElementById("live_ratio").innerHTML = (Math.round(document.getElementById("live_invites").innerHTML / document.getElementById("live_accepts").innerHTML * 100) / 100).toFixed(2);
}
}
For some reason, it doesn't work how it is now, it just doesn't invoke the document.getElementById("live_ratio").innerHTML line, and doesn't update the span. However, if I uncomment the alert() above, it works perfectly! :blink: Any ideas? Also, it has to be in the while() statement because the 3 id's it checks have loaded, are loaded via AJAX.
That is the thing, how many of these people don't realise they are using AI? If they use Google Search they have no choice but to use AI. So yes, maybe half of American adults do use and I expect a lot of Uk adults do to, but I bet most of them don't realise it.
Myself, i avoid the rubbish.
Question
-Alex-
The following code divides 'invites' by 'accepts' and then rounds it to 2 decimal places, always keeping 2 decimal places even if the answer is 8.00
This is the code I am using:
function live_ratio() { // Check that the invites, accepts, and ratio spans have loaded while(!document.getElementById("live_invites").innerHTML || !document.getElementById("live_accepts").innerHTML || !document.getElementById("live_ratio").innerHTML) { //alert('blah'); document.getElementById("live_ratio").innerHTML = (Math.round(document.getElementById("live_invites").innerHTML / document.getElementById("live_accepts").innerHTML * 100) / 100).toFixed(2); } }For some reason, it doesn't work how it is now, it just doesn't invoke the document.getElementById("live_ratio").innerHTML line, and doesn't update the span. However, if I uncomment the alert() above, it works perfectly! :blink: Any ideas? Also, it has to be in the while() statement because the 3 id's it checks have loaded, are loaded via AJAX.
Thanks all!
Link to comment
https://www.neowin.net/forum/topic/705392-js-simple-function-not-working-unless-i-do-alert-first/Share on other sites
13 answers to this question
Recommended Posts