A function uses the jquery.ajax()
method to get data from server and use it as return value. If I use async=true the function returns prematurely empty value. If I use async=false the wait for the function is too long. I tried toggle div showing spinning clock right before the request, but the div does not appear until the request is over.
函数使用jquery.ajax()方法从服务器获取数据并将其用作返回值。如果我使用async=true,函数会提前返回空值。如果我使用async=false,那么函数的等待时间就太长了。我尝试在请求之前切换div显示旋转时钟,但是div直到请求结束才出现。
Any advice on how not to lock the browser or show wait icon/symbol/text?
如何不锁定浏览器或显示等待图标/符号/文本?
3 个解决方案
#1
2
You can't. Synchronous requests will lock up the browser, including any animations you might have going or dom modifications that might be pending - that's why they're discouraged. Chances are, you're trying to return a value from the function firing off the ajax request, which WILL NOT WORK for async requests - modify your logic to handle the response processing in the success callback, and all will be well...
你不能。同步请求将锁住浏览器,包括您可能正在执行的任何动画或可能正在等待的dom修改——这就是为什么不鼓励它们。很有可能,您正在尝试从触发ajax请求的函数返回一个值,这个值对异步请求不起作用——修改您的逻辑,在success回调中处理响应处理,一切都会很好……
#2
1
For not locking the browser you should use async request, maybe your code or ajax response needs refactoring.
为了不锁定浏览器,您应该使用异步请求,也许您的代码或ajax响应需要重构。
But if you still not wanting to use async requests, you can do something like this:
但是如果您仍然不希望使用异步请求,您可以这样做:
- Insert the spinning clock and show
- 插入旋转时钟并显示
- When ready (and I mean a callback), make the ajax request
- 准备好(我指的是回调)后,发出ajax请求
- Finally, remove the clock
- 最后,把时钟
#3
1
one way is to put an image (Loading... gif animated). Show it before you send the ajax. Then hide it on success. Just like below.
一种方法是放置一个图像(加载……)gif动画)。在发送ajax之前显示它。然后隐藏成功。就像下面一样。
$.ajax({
url: 'ajax/test.html',
beforeSend : function(){
$('#LodingImg').show(); // show image..
},
success: function(data) {
$('#LodingImg').hide(); // hide image
$('.result').html(data);
alert('Load was performed.');
}
});
#1
2
You can't. Synchronous requests will lock up the browser, including any animations you might have going or dom modifications that might be pending - that's why they're discouraged. Chances are, you're trying to return a value from the function firing off the ajax request, which WILL NOT WORK for async requests - modify your logic to handle the response processing in the success callback, and all will be well...
你不能。同步请求将锁住浏览器,包括您可能正在执行的任何动画或可能正在等待的dom修改——这就是为什么不鼓励它们。很有可能,您正在尝试从触发ajax请求的函数返回一个值,这个值对异步请求不起作用——修改您的逻辑,在success回调中处理响应处理,一切都会很好……
#2
1
For not locking the browser you should use async request, maybe your code or ajax response needs refactoring.
为了不锁定浏览器,您应该使用异步请求,也许您的代码或ajax响应需要重构。
But if you still not wanting to use async requests, you can do something like this:
但是如果您仍然不希望使用异步请求,您可以这样做:
- Insert the spinning clock and show
- 插入旋转时钟并显示
- When ready (and I mean a callback), make the ajax request
- 准备好(我指的是回调)后,发出ajax请求
- Finally, remove the clock
- 最后,把时钟
#3
1
one way is to put an image (Loading... gif animated). Show it before you send the ajax. Then hide it on success. Just like below.
一种方法是放置一个图像(加载……)gif动画)。在发送ajax之前显示它。然后隐藏成功。就像下面一样。
$.ajax({
url: 'ajax/test.html',
beforeSend : function(){
$('#LodingImg').show(); // show image..
},
success: function(data) {
$('#LodingImg').hide(); // hide image
$('.result').html(data);
alert('Load was performed.');
}
});