在函数运行之前等待计时器完成
我认为这是相当基本的,但我似乎无法在网上找到。这可以是 JavaScript 或 jquery。
我需要创建一个大约 1-2 秒的计时器。 同时另一个功能是使用ajax将数据传递到服务器端php文件。当响应文本返回时,它会将其显示在页面上。
目前我正在运行 ajax 函数,该函数完成所需的时间约为 0.1 秒。但这使得页面看起来非常跳跃,因为内容改变了 css 样式,而 ajax 正在等待响应,然后在返回时返回到原始状态(希望这是有道理的)。
无论如何,为了解决这个问题,我希望该函数在显示响应文本之前检查计时器是否已结束。
目前我可以获得它的唯一方法是创建一个间隔计时器一秒钟并在完成时运行 ajax 函数,但这并不理想,因为即使向服务器发出请求,查看器也必须等待额外的一秒或 2 秒占用该时间来完成。
希望这一切都有意义非常感谢您抽出时间。
克里斯
I think this fairly basic but I can't seem to find one on-line. This can be in JavaScript or jquery.
I need to create a timer for about a 1-2 seconds.
Meanwhile another function is using ajax to pass data to a server side php file. When the response text gets back it displays it on the page.
At the moment I have the ajax function running and the time taken for the function to complete is about 0.1 seconds. But this makes the page look really jumpy as the content changes css styles while the ajax is waiting for a response and then back to the original on return (hope that makes sense).
Anyway to combat this I would like the function to check if the timer has ended before displaying the response text.
The only way I can get it at the moment is by creating a interval timer for a second and running the ajax function when that completes, but this is not ideal as the viewer MUST wait the extra second or 2 even if the request to the server takes over that time to complete.
Hope All Of That Makes Sense & Thanks Very Much For Your Time.
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好将函数作为“成功”处理程序附加到 AJAX 调用,而不是使用固定计时器。如何附加它取决于哪个库(如果有):
jQuery 1.4 样式(在 1.5 中仍然有效)
jQuery 1.5 样式
DOM 样式
You're better off attaching your function as a "success" handler to your AJAX call rather than using a fixed timer. How you attach it depends on which library, if any:
jQuery 1.4 style (still works in 1.5)
jQuery 1.5 style
DOM style
如果计时器是您唯一的选择,我会使用
setTimeout
或 jQuery.delay()
。I would use a
setTimeout
or the jQuery.delay()
if a timer is your only option.