使用JavaScript setInterval函数
我发现了很多与使用JavaScript setInterval()函数有关的问题,找到了答案并实现了它们,但我的运气不好,没有一个能成功。我可能犯了一些愚蠢的错误,但我现在无法发现。下面是我的代码片段使用JavaScript setInterval函数,javascript,jquery,setinterval,Javascript,Jquery,Setinterval,我发现了很多与使用JavaScript setInterval()函数有关的问题,找到了答案并实现了它们,但我的运气不好,没有一个能成功。我可能犯了一些愚蠢的错误,但我现在无法发现。下面是我的代码片段 $.ajax({ url: "https://api.dropbox.com/1/oauth/request_token", data:`{"oauth_version":"1.0","oauth_signature_method":"PLAINTEXT","oauth_consumer_key
$.ajax({
url: "https://api.dropbox.com/1/oauth/request_token",
data:`{"oauth_version":"1.0","oauth_signature_method":"PLAINTEXT","oauth_consumer_key":"consumer_key","oauth_signature":"signature&"}`,
type: 'POST',
async: false,
cache: false,
dataType: 'text',
contentType: "application/x-www-form-urlencoded",
processData: true,
success: function(requestInfo)
{
console.log("requestInfo: "+requestInfo);
requestInfo = "http://localhost/?"+requestInfo;
var oauth_request_token = processQueryStringData(requestInfo, 'oauth_token'); //a regex function that parses oauth_token from requestInfo
var oauth_request_token_secret = processQueryStringData(requestInfo, 'oauth_token_secret');//a regex function that parses oauth_token_secret from requestInfo
console.log("oauth_token_secret: "+oauth_request_token_secret);
console.log("oauth_request_token: "+oauth_request_token);
var url = "<url-to-redirect to dropbox alongwith callback url>";
var win = window.open(url, 'DropBox Auth', 'width=800, height=600');
var pollTimer = window.setInterval(function() {
try {
console.log("URL ===== : " + win.document.URL);
if(**some condition is true**)
{
window.clearInterval(pollTimer);
// some code that i need to execute to get the authorize token and then the access tokens.
}
}
catch(e)
{
}
}, 1000);
});
此时,pollTimer是全局的。另外,我只看到了一次log语句,然后html页面被刷新。我无法理解我错在哪里。可能是一个愚蠢的错误,但却无法解决。请帮忙
注意:此代码存在于html文件中包含的.js文件中的函数中。这里就是问题所在。在setInterval中运行函数时,即使在ajax方法中调用setInterval,它也是完全不同的上下文。因此,我建议您先检查*某些条件为true*语句。如果“pollTimer”变量是在编写的代码中定义的,则它不是全局变量。它是“success”回调中的局部变量。(只要在同一个函数中定义了“事件”函数,就可以了。)能否在
catch
块中记录e
?你可能隐藏了一个错误;例如,根据url
的值,可能不允许您阅读win.document.url
。如果在那里抛出错误,console.log
将永远不会被调用。@Pointy-你是对的,在上面的问题中,“pollTimer”不是全局的,但当我创建“event”函数时,我将变量设置为全局的。@PaulRoub-我将尝试记录并返回给你。您好@PaulRoub,你是对的。我记录了这条消息,上面写着“访问财产文档的权限被拒绝”。我将尝试搜索错误发生的原因。如果你能帮忙,请告诉我。当然可以。我会试试的。我会让你知道的。我得到了拒绝许可的例外。正如@PaulRob所说,我将阅读同一原产地政策。谢谢你的帮助。
var pollTimer = window.setInterval(event,1000);