如何响应Pebble的SetInterval中的按钮点击?

时间:2014-11-15 21:47:12

标签: javascript jquery pebble-js

我一直试图通过SetInterval在Pebble中使用Pebble.JS在应用程序中实现秒表功能。我知道不推荐,但我现在不需要那么高的精度。

秒表将启动,但我无法停止秒表。我进行了设置,以便按下选择它应该移动到新的屏幕。

wind.on('click', 'select', function(e){
    var wind2 = new UI.Window();
    var textfield = new UI.Text({
      position: new Vector2(0, 0),
      size: new Vector2(144, 30),
      font: 'gothic-24-bold',
      text: 'Timer Started!',
      textAlign: 'center'
    });
    wind2.add(textfield);
    var textfield2 = new UI.Text({
       position: new Vector2(0, 60),
       size: new Vector2(144, 30),
       font: 'gothic-24-bold',
       text: 'PlaceHolder',
       textAlign: 'center'
     });
    wind2.add(textfield2);
    wind2.show();
    var timerID = 0;
    window.clearInterval(timerID);
    timerID = window.setInterval(function(){clockt(textfield2)},1000);

    wind2.on('click', 'select', function(e){
        timerID.clearInterval();
        var finaltime = convert(time1);
        seconds = pad(finaltime[0], 2);
        minutes = pad(finaltime[1], 2);
        var strfinaltime = "Total Time: \n" + hours + ":" + minutes + ":" + seconds +"\n" + time1
        var wind3 = new UI.Window();
        var textfield3 = new UI.Text({
            position: new Vector2(0, 50),
            size: new Vector2(144, 30),
            font: 'gothic-24-bold',
            text: strfinaltime,
            textAlign: 'center'
        });
        wind3.add(textfield3);
        wind3.show();
        time1 = 0;
    });
  });

1 个答案:

答案 0 :(得分:0)

我想我发现了这个问题,即使你不清楚你的意思并没有停止。你用两种不同的方式调用了clearInterval。尝试将第二个改为第一个。您的代码现在应该如下所示:

var timerID = 0;
window.clearInterval(timerID);
timerID = window.setInterval(function(){clockt(textfield2)},1000);

wind2.on('click', 'select', function(e){
    window.clearInterval(timerID);  // was previously timerID.clearInterval();
    var finaltime = convert(time1);