gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:11:57 25 4
gpt4 key购买 nike

我正在为 JavaScript 编写代码。在其中我尝试使用 AJAX 检查远程 asp.net 页面(aspx)连接。但我也想检查这个调用将仅持续 2 分钟的条件10 秒时间间隔。不是那样的,而是像我想的那样的逻辑,如果标志=true 如果秒 < 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

}

最佳答案

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

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 预过滤器,也可以添加在每次递归时添加的变量。

关于javascript - 在特定时间间隔后调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com