在特定时间间隔后调用javascript函数

时间:2023-02-09 01:21:24

I am writing code for JavaScript.In which I am trying to check the remote asp.net page(aspx) connection using AJAX.But also I want to check the condition that, this call will continue for 2 Minute only and with 10 sec time intervals. not that but like that logic I am thinking, If flag=true if seconds < 120 setInterval("GetFeed()", 2000);

我正在为JavaScript编写代码。其中我试图使用AJAX检查远程asp.net页面(aspx)连接。但是我想检查一下条件,这个调用将继续2分钟而且持续10秒间隔。不是那样,而是像我想的那样逻辑,如果flag = true,如果秒<120 setInterval(“GetFeed()”,2000);

can anybody please help me for that.

任何人都可以帮助我。

Here is my code of Connection check,

这是我的连接检查代码,

            var learnerUniqueID1;
        var flag='true';

        function fnCheckConnectivity(coursId) 
        {
            //here will the remote page url
            var url = 'http://<%=System.Web.HttpContext.Current.Request.UrlReferrer.Host+System.Web.HttpContext.Current.Request.ApplicationPath%>/TestConn.aspx'; 
            //alert(url);
            if (window.XMLHttpRequest) {
                learnerUniqueID1 = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) {
                learnerUniqueID1 = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else
            {
                alert("Your browser does not support XMLHTTP!");
            }
            learnerUniqueID1.open("POST", url, true);
            learnerUniqueID1.onreadystatechange = callbackZone;
            learnerUniqueID1.send(null);
        }

        function callbackZone() 
        {
            if (learnerUniqueID1.readyState == 4) 
            {
                if (learnerUniqueID1.status == 200) 
                {
                    //update the HTML DOM based on whether or not message is valid
                    parseMessageZone();
                }
                else
                {   
                    flag='false';                       
                    alert('We have detected a break in your web connection, \n please login again to continue with your session');
                }
            }
        }

        function parseMessageZone() 
        {
            var result = learnerUniqueID1.responseText;
        }        

        function makePayment(obj)
        {
            try
            {
                var Id = obj.attributes["rel"].value
                //alert(Id);
                var res=confirm('Want to Continue.');
                if(res == true)
                {
                    startRequest(Id);
                    //return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Error)
            {

            }
        }

        function startRequest(Id)
        {
            var milliseconds = 10000;
            currentDate = new Date();
            // start request
            pollRequest(milliseconds, false, currentDate, 120000,Id);
        }

        function pollRequest(milliseconds, finshed, date, timeout,Id)
        {
            if((new Date()).getTime() > date.getTime()+timeout)
            {
                // 2-minute timeout passed
                return;
            }
            if(!finished){
                  setTimeout(function(){
                     if(//check backend to see if finished)     //which method will go here
                     {   
                        fnCheckConnectivity(coursId);
                         pollRequest(milliseconds, true, date, timeout,Id);
                     }
                     else{
                         pollRequest(milliseconds, false, date, timeout,Id)
                     }
                  }, milliseconds);
                  return;
            }
            // when code reaches here, request has finished

        }

4 个解决方案

#1


1  

I'm not sure I understand correctly, but if you are trying to poll the status of a request you made every ten seconds, why not try something like this?

我不确定我是否理解正确,但是如果你试图轮询你每10秒做出一次请求的状态,为什么不尝试这样的事情呢?

function startRequest(){
    var milliseconds = 10000;
    currentDate = new Date();
    timeout = 120000;
    // start request
    pollRequest(milliseconds, false, currentDate, timeout);
}

function pollRequest(milliseconds, finshed, date, timeout){

    if((new Date()).getTime() > date.getTime()+timeout){
        // 2-minute timeout passed
        return;
    }

    if(!finished){
          setTimeout(function(){
             if(//check backend to see if finished){
                 pollRequest(milliseconds, true, date, timeout);
             }
             else{
                 pollRequest(milliseconds, false, date, timeout)
             }
          }, milliseconds);
          return;
    }

    // when code reaches here, request has finished
}

Please note this is a recursive function and browsers have a recursion limit. Also, regarding the 2 minute timeout, you can either set the timeout as an ajax prefilter or add a variable that is added on each recursion.

请注意这是一个递归函数,浏览器有一个递归限制。此外,关于2分钟超时,您可以将超时设置为ajax预过滤器,或者添加在每次递归时添加的变量。

#2


1  

If I understand correctly to want to do a task for 2 minutes every 10 seconds.

如果我理解正确,每10秒钟要做2分钟的任务。

var interval = setInterval(function(){
// do stuff
},10000); // execute every 10 seconds

setTimeout(function(){
    clearInterval(interval);
},120000); // Remove interval after 2 minutes

#3


0  

You don't want a script running continuously on a server/client. You should look up chronjob for linux or scheduler for windows.

您不希望脚本在服务器/客户端上连续运行。你应该查找linux的chronjob或windows的调度程序。

I didn't give you a proper solution, just an alternative. For what you seem to want to do, however, this seems like the right tool.

我没有给你一个合适的解决方案,只是另一种选择。但是,对于您似乎想要做的事情,这似乎是正确的工具。

#4


0  

Instead of a push request that you're describing, in which the client user waits for a certain amount of time and then makes a request, you should use a pull request.

您应该使用拉取请求,而不是您正在描述的推送请求,其中客户端用户等待一段时间然后发出请求。

To do that, you can make an AJAX call to the sever, and then the sever can wait (and possibly do something else while waiting) and then return something to the user when data is ready.

为此,您可以对服务器进行AJAX调用,然后服务器可以等待(并且可能在等待时执行其他操作),然后在数据准备好后向用户返回一些内容。

#1


1  

I'm not sure I understand correctly, but if you are trying to poll the status of a request you made every ten seconds, why not try something like this?

我不确定我是否理解正确,但是如果你试图轮询你每10秒做出一次请求的状态,为什么不尝试这样的事情呢?

function startRequest(){
    var milliseconds = 10000;
    currentDate = new Date();
    timeout = 120000;
    // start request
    pollRequest(milliseconds, false, currentDate, timeout);
}

function pollRequest(milliseconds, finshed, date, timeout){

    if((new Date()).getTime() > date.getTime()+timeout){
        // 2-minute timeout passed
        return;
    }

    if(!finished){
          setTimeout(function(){
             if(//check backend to see if finished){
                 pollRequest(milliseconds, true, date, timeout);
             }
             else{
                 pollRequest(milliseconds, false, date, timeout)
             }
          }, milliseconds);
          return;
    }

    // when code reaches here, request has finished
}

Please note this is a recursive function and browsers have a recursion limit. Also, regarding the 2 minute timeout, you can either set the timeout as an ajax prefilter or add a variable that is added on each recursion.

请注意这是一个递归函数,浏览器有一个递归限制。此外,关于2分钟超时,您可以将超时设置为ajax预过滤器,或者添加在每次递归时添加的变量。

#2


1  

If I understand correctly to want to do a task for 2 minutes every 10 seconds.

如果我理解正确,每10秒钟要做2分钟的任务。

var interval = setInterval(function(){
// do stuff
},10000); // execute every 10 seconds

setTimeout(function(){
    clearInterval(interval);
},120000); // Remove interval after 2 minutes

#3


0  

You don't want a script running continuously on a server/client. You should look up chronjob for linux or scheduler for windows.

您不希望脚本在服务器/客户端上连续运行。你应该查找linux的chronjob或windows的调度程序。

I didn't give you a proper solution, just an alternative. For what you seem to want to do, however, this seems like the right tool.

我没有给你一个合适的解决方案,只是另一种选择。但是,对于您似乎想要做的事情,这似乎是正确的工具。

#4


0  

Instead of a push request that you're describing, in which the client user waits for a certain amount of time and then makes a request, you should use a pull request.

您应该使用拉取请求,而不是您正在描述的推送请求,其中客户端用户等待一段时间然后发出请求。

To do that, you can make an AJAX call to the sever, and then the sever can wait (and possibly do something else while waiting) and then return something to the user when data is ready.

为此,您可以对服务器进行AJAX调用,然后服务器可以等待(并且可能在等待时执行其他操作),然后在数据准备好后向用户返回一些内容。