0

I've got this simple countdown timer -

$(document).ready(function() {

var Clock = {
    totalSeconds: 800,

    start: function () {
        var self = this;

        this.interval = setInterval(function () {
            self.totalSeconds -= 1;

            $("#hour").text(Math.floor(self.totalSeconds / 3600));
            $("#min").text(Math.floor(self.totalSeconds / 60 % 60));
            $("#sec").text(parseInt(self.totalSeconds % 60));
        }, 1000);
    },

    pause: function () {
        clearInterval(this.interval);
        delete this.interval;
    },

    resume: function () {
        if (!this.interval) this.start();
    }
};

Clock.start();

$('#pauseButton').click(function () { Clock.pause(); });
$('#resumeButton').click(function () { Clock.resume(); });


});

I really only want a pause/resume button as one thing

how do I turn

$('#pauseButton').click(function () { Clock.pause(); });

so it toggles the pause/resume command

EDIT....

how would I get a CMS (PHP) to decide what the totalSeconds is? As you can see it's currently going to be in a .js file so I guess I need the totalSeconds to be added at the bottom of the html/php page and edited/amended by the cms inputed?

Stuart Robson
  • 1,149
  • 4
  • 22
  • 42

2 Answers2

1

If your ok with using jquery ui, you could use a checkbox to toggle the states

html

<input type="checkbox" id="pauseResume" /><label for="pauseResume"></label>

javascript

$(document).ready(function() {
var Clock = {
    totalSeconds: 800,

    start: function () {
        var self = this;

        this.interval = setInterval(function () {
            self.totalSeconds -= 1;

            $("#hour").text(Math.floor(self.totalSeconds / 3600));
            $("#min").text(Math.floor(self.totalSeconds / 60 % 60));
            $("#sec").text(parseInt(self.totalSeconds % 60));
        }, 1000);
    },

    pause: function () {
        clearInterval(this.interval);
        delete this.interval;
    },

    resume: function () {
        if (!this.interval) this.start();
    }
};

Clock.start();

$('#pauseResume').click(function () {  
    if ($(this).is(":checked"))
    {
        Clock.pause();
        $(this).next('label').text("Resume");
    }
    else
    {
        Clock.resume();
        $(this).next('label').text("Pause");
    }
});
$('#pauseResume').button();
$('#pauseResume').next('label').text("Pause");

});

http://jsfiddle.net/axrwkr/6usZ2

here is one that doesn't use jquery ui, you would have to style the checkbox yourself

http://jsfiddle.net/axrwkr/qv62T

if you would prefer to only toggle between the two functions have a look the following question

jquery click / toggle between two functions

Community
  • 1
  • 1
0

The easiest way I can see from your code is to check whether the interval ID exists when you click the pause button...

$('#pauseButton').click(function () {
    if (Clock.interval) {
        Clock.pause();
    } else { 
        Clock.resume();
    }
});

if the ID is undefined then it knows it's already paused.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67