尝试使用 JavaScript 每 3 分钟调用一次函数

发布于 2024-08-05 15:17:13 字数 448 浏览 3 评论 0原文

我有一个返回整数的函数(社区网站上用户的当前声誉评分)。这个数字通常会根据他们的评论和提交的投票方式而上升或下降。我想每 30 秒左右“轮询”一次,看看它是否发生了变化,如果是,则更新我显示的数字。

在另一个 StackOverflow 线程中,我发现这个 JavaScript 片段看起来很有用:

function listen() {
  $.get("/mylongrequestfile", {}, function(data) {
     $("#mydiv").html(data);
     listen(); // then launch again
  }));
};

Do I just replacement /mylongrequestfile with my function?我正在尝试这样做,但效果不太好。如何使用此代码或其他代码片段每 30 秒获取并显示此值?

I have a function that returns an integer (the current reputation score for a user on a community site). That number is often going up or down based on how their comments and submissions are voted on. I'd like to "poll" it every 30 seconds or so to see if it's changed, and if so, update the number that I'm displaying.

In another StackOverflow thread, I found this JavaScript snippet that looked useful:

function listen() {
  $.get("/mylongrequestfile", {}, function(data) {
     $("#mydiv").html(data);
     listen(); // then launch again
  }));
};

Do I just replace /mylongrequestfile with my function? I'm trying that but it's not working so well. How do I use this code, or some other snippet, to grab and display this value every 30 seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

眼波传意 2024-08-12 15:17:13

您可以使用

window.setInterval

重复调用

函数,每次调用之间有固定的时间延迟调用该函数。

var intervalID = window.setInterval(yourfunctionname, 300);

这会延迟 300 毫秒执行。

回调参数

setInterval() 会将回调被调用的延迟毫秒数传递给回调函数,如果它需要其他参数作为参数,这可能会造成混淆。要避免该问题,请使用匿名函数来调用回调。

如果您需要将参数传递给回调函数,但需要它在 Internet Explorer 中工作,则可以使用相同的技术,因为 Internet Explorer 不支持使用 setInterval() 发送其他参数。

var intervalID = setInterval(function() { YourFunction(); }, 300);

You can use

window.setInterval

which

Calls a function repeatedly, with a fixed time delay between each call to that function.

var intervalID = window.setInterval(yourfunctionname, 300);

This executes in a delay of 300 milliseconds.

Callback arguments

setInterval() will pass the number of milliseconds late the callback was called into the callback function, which can confuse it if it expects something else as an argument. To sidestep that problem, use an anonymous function to call your callback.

The same technique can be used if you need to pass an argument to your callback function, but need it to work in Internet Explorer, which doesn't support sending additional parameters with setInterval().

var intervalID = setInterval(function() { YourFunction(); }, 300);
混浊又暗下来 2024-08-12 15:17:13
var listener = function () {
$.get("http://www.domain.tld/script/", {}, function(data) {
    $("#mydiv").html(data);
}));
};

var interval = setInterval(listener, 30000);
var listener = function () {
$.get("http://www.domain.tld/script/", {}, function(data) {
    $("#mydiv").html(data);
}));
};

var interval = setInterval(listener, 30000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文