Así que tengo un objeto JSON grande que estoy volviendo del servidor, luego construyendo una datatable desde él y mostrándolo en el formulario. Esto generalmente toma unos segundos ... así que estaba pensando en una barra de carga. Tengo la lógica detrás de la barra de carga, sin embargo, el ciclo que crea los datos hmtl bloquea el navegador y no puedo llamar al elemento que necesito actualizar.Cómo hacer Threading en Javascript
Aquí es mi función para hacer esto:
function buildDataTable(db_table, container_id) {
var $pb = $("<div id=\"progress-bar\"></div>");
$(container_id).html($pb);
$pb.progressbar({
value: 0
});
$.post("post location", {
view: "all"
}, function (data) {
var headers = "";
var contents = "";
var jsonObject = $.parseJSON(data);
var tik = Math.round(jsonObject.length/100);
for (key in jsonObject[0]) {
headers += "<th>" + key.replace(" ", " ") + "</th>";
}
for (i in jsonObject) {
contents += "<tr>";
for (j in jsonObject[i]) {
contents += "<td class=\"border-right\">" + jsonObject[i][j] + "</td>";
}
contents += "</tr>";
if(Math.round(i/tik) == i/tik) {
/* if I run the alert (between popups) i can see the progressbar update, otherwise I see no update, the progressbar appears empty then the $(container_id) element is updated with the table i've generated */
alert('');
$pb.progressbar("value",i/tik);
}
}
var html = "<table cellpadding=\"5\" cellspacing=\"0\"><thead><tr>" + headers + "</tr></thead><tbody>" + contents + "</tbody></table>";
$(container_id).html(html);
$(container_id).children("table:first").dataTable({
"bJQueryUI": true,
"sScrollX": "100%"
});
});
}
JavaScript tiene un solo subproceso. Tendrá que dividir su trabajo en pedazos y llamarlos en secuencia usando "setTimeout" para permitir que la GUI se actualice durante el procesamiento, pero aun así el navegador aún parecerá poco receptivo. – maerics
Entonces, ¿es mejor simplemente usar un gif animado y no mostrar el progreso real? – rlemon
@maerics: Pensé que las solicitudes asíncronas abrían un nuevo hilo? – Anthony