如何使用ajaxStart显示加载微调器?

编程入门 行业动态 更新时间:2024-10-04 15:36:11
本文介绍了如何使用ajaxStart显示加载微调器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个网页,该网页使用命令shell_exec运行python脚本.我想要一个加载微调器,在Python脚本运行时显示请在此页面加载时等待"之类的消息,然后在完成后将其余的HTML显示出来.

我在 stackoverflow/a/68503/4630491 上找到了一个很好的解决方案 但是我对ajax还是陌生的,以至于我不知道如何使用该解决方案.我尝试做

<div id="loadingDiv">Please wait while this page loads.</div> <script>var $loading = $('#loadingDiv').hide(); $(document) .ajaxStart(function () { $loading.show(); }) .ajaxStop(function () { $loading.hide(); }); </script>

但是这没有用.我需要调用ajax来执行ajaxStart吗?我怎么称呼它?我应该用Ajax代码包装shell_exec吗?

谢谢.

解决方案

每当要发送Ajax请求时,jQuery都会检查是否还有其他未完成的Ajax请求.如果没有正在进行的操作,则jQuery会触发ajaxStart事件.

具有如下所示的加载gif图片

<div id="loading"> <img src="loading.gif" /> </div>

首先隐藏此加载div(因为要发送ajax请求时必须显示加载图像).

<script> var $loading = $('#loading').hide(); //Attach the event handler to any element $(document) .ajaxStart(function () { //ajax request went so show the loading image $loading.show(); }) .ajaxStop(function () { //got response so hide the loading image $loading.hide(); }); </script>

有关更多信息,请参见 jQuery文档

我需要调用ajax来执行ajaxStart吗?我怎么称呼它?

是的,当您触发ajax请求时,只有ajaxStart会被自动触发.

对于ajax,jquery有多种方式,下面是我给load函数提供的方法.

$( ".result" ).load( "some_file.py" );

some_file.py输出将插入类名称为result的div中.

要触发加载事件,您可以根据需要使用按钮单击或任何其他操作.

$( ".trigger" ).click(function() { $( ".result" ).load( "some_file.py" ); });

I have a webpage that runs a python script with the command shell_exec. I'd like for a loading spinner, the 'Please wait while this page loads' sort of message, to show while the python script is running, then after it is done for the rest of the echo'd HTML to show.

I found what seems like a good solution at stackoverflow/a/68503/4630491 but I am so new to ajax that I don't know how to use the solution. I tried doing

<div id="loadingDiv">Please wait while this page loads.</div> <script>var $loading = $('#loadingDiv').hide(); $(document) .ajaxStart(function () { $loading.show(); }) .ajaxStop(function () { $loading.hide(); }); </script>

but this did not work. Do I need to call ajax to execute the ajaxStart? How would I call it? Should I wrap the shell_exec in ajax code?

Thanks a bunch.

解决方案

Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event.

Have a loading gif image like shown below

<div id="loading"> <img src="loading.gif" /> </div>

First hide this loading div(because loading image have to be shown when ajax request is about to sent).

<script> var $loading = $('#loading').hide(); //Attach the event handler to any element $(document) .ajaxStart(function () { //ajax request went so show the loading image $loading.show(); }) .ajaxStop(function () { //got response so hide the loading image $loading.hide(); }); </script>

For more see at jQuery documentation

Do I need to call ajax to execute the ajaxStart? How would I call it?

Yes when you triggered a ajax request then only ajaxStart will get triggered automatically.

For ajax there are multiple ways with jquery, below I am giving with load function.

$( ".result" ).load( "some_file.py" );

some_file.py output will inserted into div with class name result.

To trigger the load event you can use button click or any other as need.

$( ".trigger" ).click(function() { $( ".result" ).load( "some_file.py" ); });

更多推荐

如何使用ajaxStart显示加载微调器?

本文发布于:2023-11-03 04:40:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   加载   ajaxStart

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!