Is there any way to wait for a Dom to be updated or asynchronously update the Dom?

Is there any way to wait for a Dom to be updated or asynchronously update the Dom?



I have a simple loading bar made with css, you update the css and the bar fills, super simple.
I decided to update the bar with jQuery, works great but now I throw it into a practical environment. I have a bunch of files being downloaded and each time a file successfully downloads, it updates the position. The main problem is that it downloads the files so fast, and places the files correctly fast enough that it just doesn't update the loading bar unless I set a timeout interval of 300-400ms..it does log into console and I made an interval function that continously checks to see if a file is finished based on a global variable. No matter where I place the function to update the loading bar or how I update it, it seems the Dom will not react unless there's a big enough delay between files OR it will react at the very end (jumps to 100).



Is there any way to wait for a Dom to be updated by J's OR can you spot a problem in my code that causes this issue?



I also tried promises too but it didn't change how the browser reacts to the function.



This is all being done inside a Cordova environment but I tested it on chrome too and it works as long as the pc is powerful enough it seems.



The file Transfer function has an "on Success" too but that doesn't do anything as the Dom wont update in it until after all the downloads are done OR there's a delay



My solutions so far is to either intentionally lag the downloader, or lag it every 10 or 20 files to update the position



Edit: here's my loading bar Js


var colorInc = 100 / 3;
function setWater(myval)

var val = myval;
var waitForMe = $.Deferred();
if(val != ""
&& !isNaN(val)
&& val <= 100
&& val >= 0)

setTimeout(function()waitForMe.resolve(), 100);
var valOrig = val;
val = 100 - val;

if(valOrig == 0)

//$("#percent-box").val(0);
$(".progress .percent").text(0 + "%");

else $(".progress .percent").text(valOrig + "%");

$(".progress").parent().removeClass();
$(".progress .water").css("top", val + "%");

if(valOrig < colorInc * 1)
$(".progress").parent().addClass("red");
else if(valOrig < colorInc * 2)
$(".progress").parent().addClass("orange");
else
$(".progress").parent().addClass("green");

else

setTimeout(function()waitForMe.resolve(), 100);
$(".progress").parent().removeClass();
$(".progress").parent().addClass("green");
$(".progress .water").css("top", 100 - 67 + "%");
$(".progress .percent").text(67 + "%");
//$("#percent-box").val("");

return waitForMe.promise();
;



Dowload tracker:


var DLProgress = null;
function updateProgress()
var oldNum = 0;
DLProgress = setInterval(function()
if(!doneArts)
doneArts = true;
downloadHelper("Articles",articleSize,33.33,0);
else if(currPos >= totalSize - 1)
clearInterval(DLProgress);
goNews();
currPos = 0;
doneArticles = false;
doneJson = false;
doneArts = false;
else if(currPos >= articleSize && !doneArticles)
doneArticles = true;
downloadHelper("json",jsonSize,33.33,33.33);
else if(currPos >= articleSize + jsonSize && !doneJson)
doneJson = true;
downloadHelper("img",imgSize,33.33,66.66);

if(oldNum != currPos)
oldNum = currPos;
setWater(Math.ceil(100 * currPos / totalSize));

,5);



Download Helper :


function downloadHelper(name,size,maxPerc,startingPoint)
dataFiles[name].forEach(function(file) null; //might not work
if(getItem === null );
;



File transfer used :
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/



It Does update after each download helper has finished



Is there any way to wait for a Dom to be updated by J's OR can you spot a problem in my code that causes this issue?





The plugin is used to upload the file and you are talking about download. Are you facing issue of updating the progress bar for upload progress?
– hellojava
Aug 28 at 11:27





The plugin downloads files, yes I am facing issues updating the progress bar..it will only update when there is enough of a delay between each download.
– Ginzo Milani
Aug 28 at 15:52





Build a queue, process queue for few items at a time, show progress, process next items in queue an so on. stackoverflow.com/a/38418399/3581139
– jeetaz
Aug 30 at 6:12





I agree with @jeetaz. You need to have queue with files and hide your progress bar when queue is empty.
– Observer
Aug 30 at 13:22





.parent() is kind of slow, you might be flooding the CPU. Try caching the progress update selectors to improve perf, which might open up enough time to re-layout. you might also only update 5 times a second instead of once per file; just track the last update time, and compare to Date.now(), and return if <200...
– dandavis
Aug 31 at 6:26


.parent()




2 Answers
2



The DOM is updated each time the download is completed - it could be a problem.

We should separate upload progress and animation. When file is downloaded you should just change some kind of Model and use requestAnimationFrame recursively to animate a progress bar.



requestAnimationFrame is called 60 times per second, but will generally match the display refresh rate, paused in most browsers when running in background tabs or hidden <iframe>s in order to improve performance and battery life.


60 times per second, but will generally match the display refresh rate, paused in most browsers when running in background tabs or hidden <iframe>s in order to improve performance and battery life.





I will give this a shot and mark it as the answer while combining other solutions on here, I guess I'm over loading the Dom.
– Ginzo Milani
Sep 5 at 23:16



I think you have the case with the variable currPos. Use debug tool to mark the lines and inspect value of currPos. Somehow your code is managed to jump it 0 to articleSize.





It does work just fine when I give a big enough delay between each download.
– Ginzo Milani
Aug 28 at 15:52






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế