按间隔调用javascript函数但不是异步调用 [英] call javascript function by interval but not asynchronously

查看:70
本文介绍了按间隔调用javascript函数但不是异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为createNotificTable()的函数。这个函数使用ajax来获取数据。我必须每隔5秒调用一次。

startNotificationTable()是被调用的函数从asp服务器代码和计时器启动。



函数startNotificationTable(){



window.setInterval( createNotificTable,5000);



}



问题在于,即使每隔5秒调用createNotificTable功能没有完成,这就产生了问题。



那么如何做到这种方法?我不想把它称为精确的5秒间隔,只是想反复调用但是在完成此功能的执行之后。



但是间隔会更好,因为如果功能在2秒内完成,那么我不想立即调用它,因为它正在访问数据库。间隔会更好。



5秒或10秒会更好。



请帮忙。

I have a function named createNotificTable().This function is using ajax to fetch data.I have to call it in every 5 sec.
"startNotificationTable()" is the function which is called from asp server code and the timer starts.

function startNotificationTable() {

window.setInterval(createNotificTable, 5000);

}

problem is that,createNotificTable is called every 5 sec even if the function is not finished,which creates problem.

So how to do this approach?I don't want to call it exact 5sec interval,just want to call repeatedly but after finishing the execution of this function.

But interval will be better because if the function finished in 2 sec then I don't want to call it immediately as it is accessing database.A interval will be better.

5sec or 10 sec will be better.

Please help.

推荐答案

通过回调完成。



Done by callback.

function settimer() {
   alert('timer fired');
   setTimeout(function () {
       createNotificTable(function () {
           settimer();
       });
   }, 5000)
}







function createNotificTable(callback) {

    var tbl =


#gridNotificInbox tbody);
var empid =
("#gridNotificInbox tbody"); var empid =


#hdnempidmaster)VAL(); // 登录员工
var maxid = 0 ;
if (parseInt(
("#hdnempidmaster").val(); //logged in employee var maxid = 0; if (parseInt(


这篇关于按间隔调用javascript函数但不是异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆