如何定期触发Ajax请求?

如何定期触发Ajax请求?

<meta http-equiv="Refresh" Content="5">

此脚本每5秒重新加载或刷新页面。但是我想使用jQuery和Ajax调用来完成它。有可能吗?


ibeautiful
浏览 627回答 3
3回答

汪汪一只猫

是的,您可以使用JavaScriptsetTimeout()方法或setInterval()方法调用要运行的代码。下面是您如何使用setTimeout实现该功能的方法:function&nbsp;executeQuery()&nbsp;{ &nbsp;&nbsp;$.ajax({ &nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;'url/path/here', &nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;function(data)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;something&nbsp;with&nbsp;the&nbsp;return&nbsp;value&nbsp;here&nbsp;if&nbsp;you&nbsp;like &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); &nbsp;&nbsp;setTimeout(executeQuery,&nbsp;5000);&nbsp;//&nbsp;you&nbsp;could&nbsp;choose&nbsp;not&nbsp;to&nbsp;continue&nbsp;on&nbsp;failure...}$(document).ready(function()&nbsp;{ &nbsp;&nbsp;//&nbsp;run&nbsp;the&nbsp;first&nbsp;time;&nbsp;all&nbsp;subsequent&nbsp;calls&nbsp;will&nbsp;take&nbsp;care&nbsp;of&nbsp;themselves &nbsp;&nbsp;setTimeout(executeQuery,&nbsp;5000);});

慕虎7371278

你可以用setTimeout或setInterval.区别是-setTimeout只触发您的函数一次,然后您必须再次设置它。setInterval一次又一次地触发表达式,除非您让它停止
打开App,查看更多内容
随时随地看视频慕课网APP