简体   繁体   English

在$ .ajax调用上创建一个进度条

[英]Create a progress bar on $.ajax call

I want to display a progress bar when I click on a button to load content through ajax. 当我点击按钮通过ajax加载内容时,我想显示一个进度条。 As soon as I create the $.ajax call, the communication goes to a php file on the server which scrapes data from another file on the server. 一旦我创建$.ajax调用,通信就会转到服务器上的php文件,该文件从服务器上的另一个文件中删除数据。 And it takes a good 7-8 seconds to bring in the data. 引入数据需要7到8秒的时间。

I want to display a progress loader at the time of making the ajax request. 我想在发出ajax请求时显示进度加载器。 I was looking on the internet and couldnt find a simple solution. 我在网上看,无法找到一个简单的解决方案。 All I could find were complex upload file scripts which would take an awful while to customize to perform this simple operation. 我所能找到的只是复杂的上传文件脚本,这需要很长时间才能进行自定义以执行这个简单的操作。 If anybody can help, that'd be great. 如果有人可以提供帮助,那就太好了。 Else I'd have to make do with the spinner. 否则,我必须与旋转器一起做。

Because all other answers are just about showing a placeholder image or text or just faking it, here's how you could do it (just a description, no code - but the only hard part is determining how much of the work is actually done): 因为所有其他答案只是显示占位符图像或文本或只是伪造它,这里是你如何做到这一点(只是一个描述,没有代码 - 但唯一困难的部分是确定实际完成了多少工作):

Edit the php script you call via $.ajax to calculate the percentage of work it has completed - how you would do this is extremely dependend on the tast the script does and as such can be extremely easy (for example, when you're just iterating over an array of things to process which each take about the same time) or extremely hard (eg when most of the time is spent in one single, non-repeating call to a library or built-in function). 编辑你通过$.ajax调用的php脚本来计算它已经完成的工作的百分比 - 你如何做到这一点非常依赖于脚本所做的事情,因此非常容易(例如,当你只是迭代要处理的事物数组,每个事物需要大约相同的时间)或极其困难(例如,大部分时间花费在对库或内置函数的单次,非重复调用中)。 Save this percentage in a session variable: 将此百分比保存在会话变量中:

$_SESSION["PERCENTAGE_DONE"] = $my_calculated_percentage;

Obviously, you want to update this variable as often as is possible and reasonable. 显然,您希望尽可能经常更新此变量。

Now, create another php script which just prints out this value (plaintext would be enough for this example, but you could also use json or xml or some other format you like): 现在,创建另一个PHP脚本,只打印出这个值(明文就足够了这个例子,但你也可以使用json或xml或其他你喜欢的格式):

//initialize session, set headers etc. before this
echo $_SESSION["PERCENTAGE_DONE"]

For the javascript part, create another function that calls the new php script via ajax, reads the returned value and draws / updates the progress bar (for the drawing part you could take JustinJohns answer as a starting point). 对于javascript部分,创建另一个通过ajax调用新php脚本的函数,读取返回的值并绘制/更新进度条(对于绘图部分,您可以将JustinJohns的答案作为起点)。 Now, after you execute the main $.ajax call, use this function with setTimeout to query the server repeatedly until the task is finished. 现在,在执行主$.ajax调用之后,使用此函数和setTimeout重复查询服务器,直到任务完成。

Please try following script for Ajax progress bar: 请尝试以下Ajax进度条脚本:

<script>
function submit_form_data(){
    $("#preview").html(''); 
    $("#preview").html('<img src="loader.gif" alt="Uploading...."/> <br><p>Please wait for response.............</p>'); 
    $.post('getdata.php',function(data){$("#preview").html(data)}) }
</script>

<div id='preview'></div>

The fundamental problem is that you do not know how long the request is going to take. 根本问题在于您不知道请求需要多长时间。

This will be suggestion rather than an answer. 这将是建议而不是答案。

I assumed time will be around 7-8 seconds always. 我假设时间总是在7-8秒左右。

Make progress with loading 10 percentage on each second until 90 percentage attained. 每秒加载10个百分点,直到达到90个百分点。

After attaining wait for your ajax request to be completed and make bar width 100 percentage on success callback of ajax. 在等待你的ajax请求完成后,在成功回调ajax时使条宽100%。

You can customize with time to load percentage of bar. 您可以根据时间自定义加载条的百分比。

Please check this demo 请查看此演示

The image you used should loading animation image. 您使用的图像应该加载动画图像。

<div id="loading" >
<p>
<img src="images/loading.gif">
Please Wait
</p>
</div>

Then in the script function use the following code 然后在脚本函数中使用以下代码

<script type='text/javascript'>
function showLoading() {
$("#loading").show();
}

function hideLoading() {
$("#loading").hide();
}

</script>

Then call the showLoading and hideLoading function whereever you need in the ajax call. 然后在ajax调用中调用showLoading和hideLoading函数。 It will shows and hides the animation. 它将显示和隐藏动画。

As you have asked for a simple solution, This may help you.. Have a div with ID 'loading' with display as hidden. 正如你要求一个简单的解决方案,这可能会帮助你..有一个ID为'loading'的div,显示为隐藏。

<div id='loading' style='display:none;'><img src='loading.gif' /></div>

When the ajax request called, display the div 当调用ajax请求时,显示div

$('#loading').show();

When the response is success, again hide back the div, 当响应成功时,再次隐藏div,

$('#loading').hide();

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

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