Reorganized page when refreshed #147277
-
BodyI am working on a website that will show 1 billion digits of pi (I know its not practical but I thought it would be fun), and I'm having a bug where it will sometimes load with the strings of text switched places, so it ends up looking like this: <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>One Billion Digits of Pi</title>
<style>
body {
font-size: 20px;
font-family: "Trebuchet MS", Veranda, sans-serif;
display: inline-block;
margin: 10px;
}
.digits {
display: inline-block;
font-family: serif;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
</head>
<body>
<h1>One Billion Digits of Pi:</h1>
<p id="digits"></p>
<script>
$(document).ready(function() {
var paragraph = $("#digits");
var loadFiles = ["digits.html","digits2.html"];
$.each(loadFiles, function(index, file) {
$.get(file, function(data) {
var tempDiv = $("<div>").html(data);
var textContent = tempDiv.text();
var textNoSpaces = textContent.replace(/\s+/g, "");
paragraph.append(textNoSpaces);
});
});
});
</script>
</body>
</html> If you want to take a closer look, [this](https://github.com/Ethan-Mircheff/pibillion) is my repository with all the files. Thanks in advance for the help! Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @Ethan-Mircheff , The issue you're encountering is likely due to how the two files ( Solution:
Using callback functionHere's the modified code using <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>One Billion Digits of Pi</title>
<style>
body {
font-size: 20px;
font-family: "Trebuchet MS", Veranda, sans-serif;
display: inline-block;
margin: 10px;
}
.digits {
display: inline-block;
font-family: serif;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
</head>
<body>
<h1>One Billion Digits of Pi:</h1>
<p id="digits"></p>
<script>
$(document).ready(function() {
var paragraph = $("#digits");
var loadFiles = ["digits.html", "digits2.html"];
function loadNextFile(index) {
if (index < loadFiles.length) {
$.get(loadFiles[index], function(data) {
var tempDiv = $("<div>").html(data);
var textContent = tempDiv.text();
var textNoSpaces = textContent.replace(/\s+/g, "");
paragraph.append(textNoSpaces);
// Load the next file after the current one is processed
loadNextFile(index + 1);
});
}
}
// Start loading from the first file
loadNextFile(0);
});
</script>
</body>
</html> Explanation of changes:
Using async/await and fetch() method<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>One Billion Digits of Pi</title>
<style>
body {
font-size: 20px;
font-family: "Trebuchet MS", Veranda, sans-serif;
display: inline-block;
margin: 10px;
}
.digits {
display: inline-block;
font-family: serif;
}
</style>
</head>
<body>
<h1>One Billion Digits of Pi:</h1>
<p id="digits"></p>
<script>
async function loadDigits() {
const paragraph = document.getElementById("digits");
const loadFiles = ["digits.html", "digits2.html"];
// Load files sequentially
for (let file of loadFiles) {
try {
const response = await fetch(file);
const data = await response.text();
// Remove spaces and append to the paragraph
const textNoSpaces = data.replace(/\s+/g, "");
paragraph.innerHTML += textNoSpaces;
} catch (error) {
console.error(`Error loading file ${file}:`, error);
}
}
}
// Start loading digits once the document is ready
document.addEventListener("DOMContentLoaded", loadDigits);
</script>
</body>
</html> Explanation of changes:
This should resolve the issue of the digits getting mixed up or switched, as the files will now load sequentially and be appended in the correct order. Thanks |
Beta Was this translation helpful? Give feedback.
Hi @Ethan-Mircheff ,
The issue you're encountering is likely due to how the two files (
digits.html
anddigits2.html
) are being loaded asynchronously with the$.get()
method. Since both requests happen at the same time and you append their results to the same#digits
paragraph, it's possible that the data from the files is not being appended in the correct order or at the correct positions, leading to the text appearing "switched."Solution:
You can achieve this by using
$.get()
within a callback …