0

Есть функция

function rost() {
 setInterval(function() {
 $('#planka').animate({height: '+=50'}, 300 )

  }, 1);
}

$( document ).on( "vmousedown", "#igr", function() {
rost();
});

как ее правильно остановить?

function stop() {
    //тут остановить
    }

$( document ).on( "vmouseup", "#igr", function() {
stop();});
2
  • setInterval(function() { ... }, 1); - на фига?
    – user176262
    5 апр 2016 в 19:59
  • Логично,убрал,спасибо
    – zkolya
    5 апр 2016 в 20:03

1 ответ 1

1

Ваш вариант будет выглядеть примерно так:

var intervalTimer;

function rost() {
    intervalTimer = setInterval(function() {
        $('#planka').animate({height: '+=50'}, 300 )
    }, 1);
}

$(document).on( "vmousedown", "#igr", function() {
    rost();
});

function stop() {
    $('#planka').stop(true,true);
    clearInterval(intervalTimer);
    intervalTimer = 0;
}

$(document).on( "vmouseup", "#igr", function() {
    stop();
});
3
  • Странно,остановка не происходит
    – zkolya
    5 апр 2016 в 20:00
  • Попробуйте перед очисткой интервала выполнить $('#planka').stop( true, true ). Ответ дополнил.
    – borodatych
    5 апр 2016 в 20:03
  • Спасибо! Заработало
    – zkolya
    5 апр 2016 в 20:04

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.