如何在phonegap中实现计时器

时间:2014-05-28 20:45:07

标签: cordova

我正在尝试在我的phonegap应用程序中添加一个计时器。有没有办法实现它或任何插件可用?请帮我解决一下如何实现它。

2 个答案:

答案 0 :(得分:3)

您可以从this link

获取源代码以及计时器演示

使用以下代码实现计时器

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();
    };

});

希望这有帮助。!

答案 1 :(得分:0)

您应该尝试使用插件在后台启用连续计时器 对我来说,我目前正在使用一个名为"cordova-background-timer"的插件,但您必须注意下一个:

  1. 这是一个仅限Android的插件,不适用于ios。
  2. 该插件在后台运行,您可以选择在终止时自动“重新打开”您的应用。
    在这种情况下,您应该在应用程序打开时再次运行计时器,为此,您必须设置10秒JS计时器(通过setTimeout函数)重新启动计时器,因为应用程序需要一些时间来启动插件。
  3. 在我的情况下,并且由于我未知的原因,计时器每5分钟运行一次,尽管我用60秒定义它。
  4. 就是这样,希望这对某些人有用。

相关问题