在函数运行之前等待计时器完成

发布于 2024-11-04 20:28:43 字数 434 浏览 5 评论 0原文

我认为这是相当基本的,但我似乎无法在网上找到。这可以是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-11-11 20:28:43

您最好将函数作为“成功”处理程序附加到 AJAX 调用,而不是使用固定计时器。如何附加它取决于哪个库(如果有):

jQuery 1.4 样式(在 1.5 中仍然有效)

$.ajax({
    // your AJAX options

    success: yourFunc
});

jQuery 1.5 样式

$.ajax({
    // your AJAX options
}).done(yourFunc);

DOM 样式

// after creating your XHR

xhr.onreadystatechange = function() {
    if (this.readyState === 4) { // 4 means the request has completed
        if (this.status !== 200) { // 200 is success, so anything else...
            // log or report error
            return;
        }

        // call your other function, which uses the AJAX data
        yourFunc(this.responseText);
    }
};

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)

$.ajax({
    // your AJAX options

    success: yourFunc
});

jQuery 1.5 style

$.ajax({
    // your AJAX options
}).done(yourFunc);

DOM style

// after creating your XHR

xhr.onreadystatechange = function() {
    if (this.readyState === 4) { // 4 means the request has completed
        if (this.status !== 200) { // 200 is success, so anything else...
            // log or report error
            return;
        }

        // call your other function, which uses the AJAX data
        yourFunc(this.responseText);
    }
};
云醉月微眠 2024-11-11 20:28:43

如果计时器是您唯一的选择,我会使用 setTimeout 或 jQuery .delay()

$.ajax({
    success: function() {
        setTimout(function() {
            // styling code goes here
        }, 1000);
    }
});

I would use a setTimeout or the jQuery .delay() if a timer is your only option.

$.ajax({
    success: function() {
        setTimout(function() {
            // styling code goes here
        }, 1000);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文