I'm making a page that works like a GPS for a character in a given game. The content of the page is nothing more than a map with a marked path, and 240 images overlap this map to "tag" the character's location.
The 240 images take 40 minutes and 35 seconds to loop (go from the start point to the end point, and return to the starting point).
With the help of Sergio in this other question , I was able to adjust my code to change the images every 10 seconds based on the current time.
Now the problem I'm facing is: my javascript has gone absurdly large, with more than 40 thousand lines, is leaving the page with approximately 1.8mb, and this can cause a slowdown for some users.
Following this logic to change the images:
function mudarYujia() {
var currentHora = new Date().getHours();
var currentMinuto = new Date().getMinutes();
var currentSegundo = new Date().getSeconds();
var img = document.getElementById("mapa_movimento");
var base = "http://triviapw.hiperportal.blog.br/elysium_old/images/moves/";
var prefixo = "floresta_yujia_v2-b-";
var extensao = ".png";
if (0<= currentHora && currentHora < 1){
if (2 <= currentMinuto && currentMinuto < 3){
if (45 <= currentSegundo && currentSegundo < 55){
img.src = base + prefixo + '1' + extensao;
}}}
if (0<= currentHora && currentHora < 1){
if (3 <= currentMinuto && currentMinuto < 4){
if (5 <= currentSegundo && currentSegundo < 15){
img.src = base + prefixo + '2' + extensao;
}}}
if (0<= currentHora && currentHora < 1){
if (3 <= currentMinuto && currentMinuto < 4){
if (15 <= currentSegundo && currentSegundo < 25){
img.src = base + prefixo + '3' + extensao;
}}}
if (0<= currentHora && currentHora < 1){
if (3 <= currentMinuto && currentMinuto < 4){
if (26 <= currentSegundo && currentSegundo < 36){
img.src = base + prefixo + '4' + extensao;
}}}
if (0<= currentHora && currentHora < 1){
if (3 <= currentMinuto && currentMinuto < 4){
if (36 <= currentSegundo && currentSegundo < 46){
img.src = base + prefixo + '5' + extensao;
}}}
...
return mudarYujia;
}
setInterval(mudarYujia(), 1000); // atualizar a cada 1 segundo
The function checks the current hour, minute, and second to be able to tell which image to display at this time. All images are numbered from 1 to 240 in this format "forest_yujia_v2-b-1.png", "forest_yujia_v2-b-2.png" ...
The full code can be seen here at jsfiddle
Andtheimagessuperimposedonthemap,whichareexchanged,notjustanobjectusedtomarkthelocation,andfollowinthisformat,butinotherpositions
An important detail is that the character resumes the path (image "forest_yujia_v2-b-1.png") every day at 00 hours, 2 minutes and 35 seconds.
I tried to locate something on the internet that could help me reduce everything, but I did not get any results.