1

Currently I have a button which changes its text from Pause to Resume when clicking on it. I use jQuery and toggle for this:

$(document).ready(function() {
    $("#pause").click(function() {
      $("td").toggle();
      $(this).html($(this).html() == "Pause" ? "Resume" : "Pause");
    });
});

This all works. I also have 2 functions:

function pauseTimer() 
function startTimer()

How do I "integrate" these two functions into my toggle code? So when I hit Pause it will toggle the text to Resume AND also use the function pauseTimer() and if I hit the button again, the text will change back to Pause and also use StartTimer()?

Thanks

2
  • Did you read the docs? api.jquery.com/toggle Commented Nov 12, 2013 at 14:17
  • 1
    Just turn the ternary statement in to a normal if condition containing the two statements. Commented Nov 12, 2013 at 14:19

9 Answers 9

2
$(this).html() == "Pause" ? pauseTimer() : startTimer();

Will work. You have to change the html content of the element to Start or Pause in the functions so that the function will run alternatively according to the html of the element.

Sign up to request clarification or add additional context in comments.

Comments

1

Unless I completely misinterpreted your question this should do it:

$("#pause").click(function() {
  $("td").toggle();
  if($(this).html() == "Pause")
  {
      $(this).html("Resume");
      pauseTimer();
  }else{
      $(this).html("Pause");
      startTimer();
  }
});

1 Comment

Thanks all, I have chosen this one, suited me best
1

Something like this ?

var doStuff = function(callback, text){
    callback();
    return text;
}
$(this).html($(this).html() == "Pause" ? doStuff(startTimer, "Resume") : doStuff(StartTimer, "Pause"));

Comments

1
var globalTimerPaused = false;
$(document).ready(function() {
    $("#pause").click(function() {
      $("td").toggle();
      $(this).html($(this).html() == "Pause" ? "Resume" : "Pause");
      if(!globalTimerPaused){ pauseTimer() } else { startTimer() };
      globalTimerPaused = !globalTimerPaused;
    });
});

1 Comment

use mine in scenarios where you have need to access the state of the timer in blocks of code outside the if statement
1

Try this

$('selector').click(function () { // on click event
  var song = $('theaudiotag');
  if(song.paused) { // if song is paused
     $(this).play(); // play it
     $(this).html('Pause'); // change text
  } else { // otherwise
     $(this).pause(); // pause it
     $(this).html('Play'); // change text
  }
}

This uses jQuery API to check for the current status of the audio tag, and then toggle its state to play or pause and at the same time. It will work cross-browser.

Comments

1

You can use the window method here like:

$("#pause").click(function () {
    $("td").toggle();
    $(this).html($(this).html() == "Pause" ? "Resume" : "Pause");
    window[$(this).html() === "Resume" ? "pauseTimer" : "startTimer"]();
});

Comments

1

You can use the jQuery is visible method:

if ($('td').is(":visible"))
{
pauseTimer();
}
else
{
startTimer()
}

Dan

Comments

1

Another option would be to do the following.

//jQuery 1.8 toggle replacement
$.fn.toggleClick = function(){
    var methods = arguments,
        count = methods.length;
    return this.each(function(i, item){
        var index = 0;
        $(item).click(function(){
            return methods[index++ % count].apply(this,arguments);
        });
    });
};

function startTime() {
    $("td").toggle();
    $(this).val("Start");
}

function pauseTime() {
    $("td").toggle();
    $(this).val("Pause");
}

$("#clickme").toggleClick(startTime,pauseTime);

JSFiddle

Comments

1
$(document).ready(function() {
$("#pause").click(function(pauseTimer(),StartTimer()) {
  $("td").toggle();
  $(this).html($(this).html() == "Pause" ? "Resume" : "Pause");
});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.