and in success function I write a lot of code for assign values to HTML elements that they fetch from database.
After all I let $(“#myDiv”).css(“display”,“block”) for show my div that contain HTML elements. but when internet speed is low, after show DIV users can see assigning values to HTML elements.
How I can show MyDIV after all values assign to HTML elements ?
I know dear. But sometimes specially when internet speed is low (for example user is downloading a huge file and work with web page) user can see when data are assigning to HTML elements and if user click on “save” button before finishing all assigns, then appear wrong and empty data on database.
How I can detect when all assigns are finished ?!
Hi @joeafshany, I don’t quite understand the problem… you say you set #myDiv to display: block after you fetched all the data and populated the HTML elements, so as @igor_g noted, the user should only see it when the content is actually ready.
Are you aware though that AJAX requests are asynchronous, i.e. the code execution doesn’t get paused until the response arrives? So this will show the div immediately without waiting for the response:
$.ajax({
// ...
success: function () { }
})
$('#myDiv').show()
The order of execution is not necessarily the same as the order of your code; you’d have to do this inside the success handler instead then:
$.ajax({
// ...
success: function (response) {
// Populate fields, then
$('#myDiv').show()
}
)
Otherwise, could you post a more complete code example at which point exactly you’re assigning the values and displaying the container div?
you show the div, not right when then request got sent.
BTW you don’t need to parse the response if you set dataType: 'json' in the $.ajax() options; this way you won’t get an error if jQuery already parsed the response according to its MIME type.