ajax10秒清除,使用setinterval每10秒调用一次ajax(calling ajax every 10 seconds using setinterval)...

使用setinterval每10秒调用一次ajax(calling ajax every 10 seconds using setinterval)

我每隔10秒使用setinterval来调用ajax,所以我的问题是,这种方式对服务器是不好的,使用setinterval在服务器端严重制造ajax效果,如果这是最好的方法,在服务器上没有效果不好侧,谢谢

i'm using setinterval to call ajax every 10 seconds so my question is,is this way are bad for server,does using setinterval make ajax effect badly on server side and if it's what is the best way to do that without effect badly on server side,thanks

原文:https://stackoverflow.com/questions/40627392

更新时间:2019-11-08 15:25

最满意答案

这意味着每10秒向服务器发出一次XHR请求,这不是一个坏习惯,因为这是一个核心要求。 但是,可以通过在服务器端应用缓存数据的机制来减少对数据库的直接命中,并且仅在数据库上应用任何CRUD操作时执行命中。

It means an XHR Request every 10 seconds to the server, which is not a bad practice, since it is a core requirement. However, there can be a solution to it by applying the mechanism of Caching Data on Server Side, to reduce direct hits to the Database, and only perform hits in case there are any CRUD Operations applied on Database.

相关问答

尝试从你的ajax成功函数中设置另一个超时: function poll() {

setTimeout( function() {

$.ajax(.......).success( function(data) {

poll(); //call your function again after successfully calling the first time.

});

}, 10000);

}

try settin

...

这意味着每10秒向服务器发出一次XHR请求,这不是一个坏习惯,因为这是一个核心要求。 但是,可以通过在服务器端应用缓存数据的机制来减少对数据库的直接命中,并且仅在数据库上应用任何CRUD操作时执行命中。 It means an XHR Request every 10 seconds to the server, which is not a bad practice, since it is a core requirement. However, there can be a solution

...

setInterval(function() {

ajxCall();

}, 10000);

试试看 setInterval(function() {

ajxCall();

}, 10000);

Try that

setTimeout(function() {

function doit() {

console.log("HERE");

}

doit(); // It's already been 10 seconds, so run it now

setInterval(doit, 25 * 1000); // Run it every 25 seconds from here on out

}, 10 * 1000); setTimeout(f

...

将callback定义为函数并调用它。 也将它作为setInterval第一个参数传递 function handler() {

alert(); //Just for demo

$('#slideshow > div:first')

.fadeOut(0)

.next()

.fadeIn(0)

.end()

.appendTo('#slideshow');

}

handler();

setInterval(handler, 10

...

我肯定会考虑缓存。 特别是如果你获得了更多用户的集合,那么每10秒发出一次AJAX请求可能会使服务器过载。 但是,如果您想保持简单,请每隔几分钟发出一次请求,以进行更新。 缓存用户,将它们生成为javascript代码,例如users = new Array(user1,user2,...)。 如果没有那么重要,你就不必继续更新页面,因为大多数用户无论如何都会在一两分钟之内离开。 如果你有一个每隔几秒钟更改一次的长列表,那么就会有足够的时间来使用AJAX进行更新,而只需依赖服务器生成的用户列表。

...

要么这样做: function update() {

//update logic belongs here

setTimeout(update, 5000 /*5secs*/);

}

setTimeout(update, 5000 /*5secs*/);

或者像这样: function update() {

//update logic belongs here

}

setInterval(update, 5000 /*5secs*/);

Got it working

...

建议/步骤/错误: 创建一个单独的函数来执行ajax请求 在页面加载时调用此函数以在加载页面时运行它 使用setInterval()每n秒调用一次该函数 id应该始终是唯一的。 您现在正在使用verification ,就像

和 您可以删除 id和language属性。 那些不是必需的。 码: function update() {

$.ajax({

url: 'update.php', //php

...

您可以为此添加一个计数器来计算您的呼叫以及何时达到120,然后调用ajax来发布数据。 var counter = 0 ;

timer.interval = setInterval(tick = function () {

/// your stuff

counter++;

if(counter == 120)

{

/// make ajax call

/// then

counter = 0;

}

}, 1000

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值