如何使纯JavaScript的一个“同步”AJAX调用,当显示“正在载入”? [英] How to display 'Loading' when making a 'synchronous' AJAX call in pure JavaScript?

查看:227
本文介绍了如何使纯JavaScript的一个“同步”AJAX调用,当显示“正在载入”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以确保结果显示,以用户,所以我做同步AJAX调用。这很简单,以显示加载指标与异步AJAX(的例子比比皆是),但是当我使用同步AJAX调用XMLHtt prequest,负载指示GIF图像不显示在所有。

I want to make sure the result is shown to user, so I make synchronous AJAX call. It's quite simple to display a 'Loading' indicator with asynchronous AJAX (the examples are everywhere), but when I use synchronous AJAX call with XMLHttpRequest, the loading indicator GIF image doesn't show up at all.

有人说,做一个同步调用(阻塞,直到有来自服务器的响应)时,是不可能显示指标。但我只是想问问,看看是否有一种方法可以做到这一点。

Some said that it's impossible to show indicator when doing a synchronous call (block until having response from server). But I just want to ask to see whether there's a way to do it.

推荐答案

这是不可能的,因为Javascript是单线程的,和同步调用模块更新到用户界面。

It's "impossible" because Javascript is single-threaded, and the synchronous call blocks updates to the UI.

不过,您可以显示动画加载图形的的启动同步AJAX调用,并成功或失败时删除它。我相信大多数的浏览器将能够继续呈现即使在技术上封锁同步调用的gif动画。

However, you may be able to display an animated 'loading' graphic before launching the synchronous AJAX call, and removing it upon success or failure. I believe most browsers will be able to continue rendering the animated gif even while technically blocked for the synchronous call.

这篇关于如何使纯JavaScript的一个“同步”AJAX调用,当显示“正在载入”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆