简体   繁体   English

jQuery getJSON回调函数在完成之前不会更新DOM

[英]jQuery getJSON callback function doesn't update the DOM until it finishes

I have a jQuery ui progressbar 我有一个jQuery ui progressbar

var item = 4;

$('<div/>', { id: 'progressBar' + item, 'class': 'ui-widget-default flpb' }).appendTo($('#test'));
$('#progressBar' + item).progressbar({ value: 0 });

here the progressbar shows up on the page 这里进度条显示在页面上

then I do a ajax call like this 然后我像这样做一个ajax电话

$.getJSON("http://" + jsonServiceUrl + "/data/for/" + item + "/from/" + $('#dtStart').val() + "/to/" + $('#dtEnd').val() + "?callback=?", 
   function (results) {
       var pbScale = 100 / results.count,
           startTime = new Date().getTime();

       $.each(result, function (r, result) {
           //here the progress bar doesn't update.
           $('<div/>', { html: " Date: " + new Date(parseInt(result.TimeOfMeasurement.substr(6))) }).appendTo('#test');
           $('#progressBar' + item).progressbar('value', r * pbScale);
       });
       console.debug('elapsed milisecs: ' + new Date().getTime() - startTime;
   });

The progressbar gets updates to "100%" after the callback function finishes. 回调函数完成后,进度条会将更新更新为“ 100%”。

EDIT: 编辑:

I'm not trying to get the progress of my json call, I'm trying to get the progress of looping through the collection of things I get back from the server. 我不是在尝试获取json调用的进度,而是在尝试遍历从服务器获取的东西的循环过程。 Next to updating the progress bar I also draw a div for every item in the collection. 在更新进度条旁边,我还为集合中的每个项目绘制了一个div。 There are upto 200 items in the collection so there is progress enough to be shown. 集合中最多有200个项目,因此进度足以显示。

Next to the progress bar not updating, the newly generated divs also don't show up until the callback function finishes. 在进度条不更新的旁边,新生成的div也不会显示,直到回调函数完成为止。

I added the timer and the processing time is about 1000 miliseconds (which must be enough to update the progressbar a couple items). 我添加了计时器,处理时间约为1000毫秒(这必须足以更新进度条中的几个项目)。

The Callback function only fires when the call is successful. 回调函数仅在呼叫成功时触发。

the 2 methods that can be used to get the solution you want would be: partial calls, have your server return only a chunk of the result, it will then fire the callback function, from within that callback fire a next ajax call to fetch the next chunk, this would however result in a few ajax calls instead of one, but you can have your progressbar working. 可以用来获得所需解决方案的2种方法是:部分调用,让服务器仅返回结果的一部分,然后将触发回调函数,从该回调函数中触发下一个ajax调用以获取在下一个代码块中,这将导致几个ajax调用而不是一个,但是您可以使进度条正常工作。

second method would be to do something with sockets and stream your data from the server to your pc, I doubt you can do something like that with regular jquery, you will need at least some plugin for that, and your server side code will also need to support it. 第二种方法是使用套接字执行某些操作,并将数据从服务器流式传输到PC,我怀疑您可以使用常规jquery进行此类操作,您至少需要一些插件,并且服务器端代码也需要支持它。 for more info take a look at socket.io and 有关更多信息,请查看socket.io和

$.each is a synchronous call, which is equivalent to a for(;;;) loop. $.each是一个同步调用,等效于for(;;;)循环。 The iteration over your results is instantaneous, and whilst the progressbar will be updating in the background, it's too quick for you to notice. results的迭代是瞬时的,尽管进度条在后台更新,但您注意到的太快了。

You want to be showing the progress of your AJAX request, not your progress of iterating over the result set. 您要显示的是AJAX请求的进度,而不是迭代结果集的进度。 Furthermore, because the $.each is synchronous, if it ever becomes large enough to warrant a progressbar for it, you'll have bigger problems to worry about; 此外,由于$.each是同步的,因此如果$.each变得足够大以至于需要为它提供进度条,那么您将有更大的问题要担心; such as the unresponsiveness of your application until the iteration completes, than showing a progress bar. 例如在迭代完成之前您的应用程序无响应,而不是显示进度条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM