0

AJAXを使用してリモートasp.netページ(aspx)接続を確認しようとしているJavaScriptのコードを書いています。しかし、この呼び出しが2分間のみ、10秒で継続するという条件も確認したい間隔。そうではありませんが、私が考えているロジックのように、If flag=true if seconds < 120 setInterval("GetFeed()", 2000);

誰でも私を助けてくれませんか。

これが私の接続チェックのコードです。

            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

4 に答える 4

1

10秒ごとに2分間タスクを実行したいということを正しく理解している場合。

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

setTimeout(function(){
    clearInterval(interval);
},120000); // Remove interval after 2 minutes
于 2012-11-28T10:09:36.007 に答える
1

正しく理解できているかどうかわかりませんが、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
}

これは再帰関数であり、ブラウザーには再帰制限があることに注意してください。また、2 分間のタイムアウトに関しては、タイムアウトを ajax プレフィルターとして設定するか、再帰ごとに追加される変数を追加することができます。

于 2012-11-28T09:39:43.517 に答える
0

クライアント ユーザーが一定の時間待機してからリクエストを行う、説明しているプッシュ リクエストの代わりに、プル リクエストを使用する必要があります。

これを行うには、サーバーへの AJAX 呼び出しを行うことができます。その後、サーバーは待機し (待機中に別のことを行う可能性があります)、データの準備ができたらユーザーに何かを返します。

于 2012-11-28T09:31:55.207 に答える
0

サーバー/クライアントでスクリプトを継続的に実行する必要はありません。Linux の場合は chronjob、Windows の場合は scheduler を検索する必要があります。

私はあなたに適切な解決策を提供しませんでした。ただし、やりたいと思われることについては、これは適切なツールのようです。

于 2012-11-28T09:28:33.937 に答える