【问题标题】:setInterval() not working after window.open() in Android在 Android 中的 window.open() 之后 setInterval() 不起作用
【发布时间】:2015-04-16 21:17:41
【问题描述】:

只是一个小问题:我正在制作一个使用 Cordova 以 Angular 开发的混合应用程序,但是当我在 $window.open() 之后使用 setInterval() 函数时,setInterval 停止工作,但在 Chrome 中,它正在工作(但它是正常,新窗口在新标签页中打开)。

这是我的代码的摘录:

$http.post(url)
.success(function(result, status) {
    var windowWithings = $window.open(result.res);
    var intervalVerif = $window.setInterval(function (){
        $http.get(url)
        .success(function(result) {
            windowWithings.close();
            $window.cancelInterval(intervalVerif);
        })
        .error(function(error) {
            console.log(error);
        });
    }, 2000);
})
.error(function(error, status) {
    console.log(status);
});

提前致谢!

编辑:

我对@9​​87654325@ 的搜索是每 2000 毫秒检查一次服务器是否从我打开的新窗口中获取信息。如果服务器没问题,我会关闭窗口以返回上一个。

当我说它不起作用时,我想说新窗口没有关闭,但如果我用警报更改代码,有时我会在新窗口有时间打开之前看到警报,但之后什么也没有

p>

对于 Chrome,我只想说,如果我在笔记本电脑上的 Chrome 中打开 Angular 应用程序,它可以工作,但如果我启动使用 Cordova 创建的 Android 应用程序,它就不能工作。

【问题讨论】:

  • 我真的不明白你想要完成什么,你应该更具体一点。 “setInterval 停止工作”——什么不工作,你期待什么。 “但在 chrome 中它可以工作”——如果你使用其他浏览器它可以工作吗?
  • 我编辑我的帖子以获取更多信息。

标签: javascript android angularjs cordova ionic-framework


【解决方案1】:

请使用 $timeout 并在注入参数中添加“$timeout”

$timeout(function (){
  $http.get(url)
  .success(function(result) {
    windowWithings.close();
    $window.cancelInterval(intervalVerif);
  })
  .error(function(error) {
    console.log(error);
  });, 2000);

【讨论】:

  • 这不是我想做的超时,而是一个间隔(请参阅我对原始帖子的编辑)。我之前尝试过使用 $interval 但问题是一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 2018-11-06
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多