0
            $('#seconds').spinner({
                spin: function(event, ui) {
                    if (ui.value >= 60) {
                        $(this).spinner('value', ui.value - 60);
                        $('#minutes').spinner('stepUp');
                        return false;
                    } else if (ui.value < 0) {
                        $(this).spinner('value', ui.value + 60);
                        $('#minutes').spinner('stepDown');
                        return false;
                    }
                }
            });

How can I name a function that includes same code above and how can I call it?

1
  • function whatever() { /*your code */ } ? Commented Mar 10, 2016 at 21:04

2 Answers 2

3

Like this should work:

$('#seconds').spinner({
  spin: doSpin
});

function doSpin(event, ui) {
  if (ui.value >= 60) {
    $(this).spinner('value', ui.value - 60);
    $('#minutes').spinner('stepUp');
    return false;
  } else if (ui.value < 0) {
    $(this).spinner('value', ui.value + 60);
    $('#minutes').spinner('stepDown');
    return false;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That is what I was looking for, thanks for quick reply.
0

Assuming you meant to extend jQuery/create a jQuery plugin, you could use:

$.fn.mySpinner = function() {
    this.spinner({
        spin: function(event, ui) {
            if (ui.value >= 60) {
                $(this).spinner('value', ui.value - 60);
                $('#minutes').spinner('stepUp');
                return false;
            } else if (ui.value < 0) {
                $(this).spinner('value', ui.value + 60);
                $('#minutes').spinner('stepDown');
                return false;
            }
        }
    });
};

$('#seconds').mySpinner();

(Although it looks like dave's answer may be closer to the mark.)

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.