【问题标题】:Run JavaScript function in phonegap ios application in background.在后台运行 phonegap ios 应用程序中的 JavaScript 函数。
【发布时间】:2014-05-23 12:17:27
【问题描述】:

我正在使用 phonegap ios 应用程序。在我的应用程序中,我使用 javascript setTimeout 函数使用递增定时器和递减定时器。当我按下 iPhone 中的菜单按钮时,定时器功能会暂停计数。当我重新打开它时,从末尾开始计数。所以有人帮我解决这个问题。如何在后台运行phonegap ios应用程序javascript函数。

【问题讨论】:

    标签: javascript ios cordova timer phonegap-plugins


    【解决方案1】:

    定时器在主线程上运行。因此,当应用程序进入后台时,它们无法运行。您可以做的是在应用程序进入后台时保存计时器计数和时间。当应用程序进入前台时,取不同的时间,并计算。

    【讨论】:

    • 感谢您的回答。确定后台后如何发现应用处于活动状态
    • 你为什么想知道?
    • 为此使用pauseresume 事件。示例:document.addEventListener('pause', app.devicePause, false);
    【解决方案2】:

    您可以从此链接获取源代码以及计时器的演示

    使用实现定时器的代码

    var count = 0;
    var timer = $.timer(function() {
        $('#counter').html(++count);
    });
    timer.set({ time : 1000, autostart : true });
    

    您还可以探索许多其他选项。例如

    var Example1 = new (function() {
    
    // Stopwatch element on the page
    var $stopwatch;
    
    // Timer speed in milliseconds
    var incrementTime = 70;
    
    // Current timer position in milliseconds
    var currentTime = 0;
    
    // Start the timer
    $(function() {
        $stopwatch = $('#stopwatch');
        Example1.Timer = $.timer(updateTimer, incrementTime, true);  
    });
    
    // Output time and increment
    function updateTimer() {
        var timeString = formatTime(currentTime);
        $stopwatch.html(timeString);
        currentTime += incrementTime;
    }
    
    // Reset timer
    this.resetStopwatch = function() {
        currentTime = 0;
        Example1.Timer.stop().once();
    };
    
    });
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2014-01-18
      • 1970-01-01
      • 2012-05-27
      • 2015-04-25
      相关资源
      最近更新 更多