How to respond to button clicks in SetInterval for Pebble?

153 views Asked by At

I've been trying to implement in an app, stopwatch functionality on Pebble using Pebble.JS via SetInterval. I know that it's not recommended but I don't need that high precision right now.

The stopwatch will start, but I can't stop the stopwatch. I set it up so that by pressing select it should move to a new screen.

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

There are 1 answers

0
Kenneth Salomon On

I think I found the problem, even though you were not clear what you meant by it not stopping. You called clearInterval two different ways. Try changing the second one to how you did the first one. Your code should look like this now for that section:

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