Javascript-форум (https://javascript.ru/forum/)
-   Ваши сайты и скрипты (https://javascript.ru/forum/project/)
-   -   вопрос о анимации (https://javascript.ru/forum/project/47144-vopros-o-animacii.html)

domius 10.05.2014 22:26

вопрос о анимации
 
Есть анимация , как сделать так чтоб анимация не вызывалась второй раз пока не окончить роботу первый setInterval ?

function animate (opts) {
	var start, move;

		start = new Date();

	move = setInterval(function () {

	var progres = (new Date - start) / opts.progres;

		if(progres >= 1) progres = 1;

		opts.init(progres);
		/* для каждого свою дыстанцию */

		if(progres == 1) clearInterval(move);
	},10)
}

function draw (to, progres, elem) {
	animate({
		to: to,
		progres: progres,
		init: function (progres) {
			elem.style.left = this.to*progres + 'px';
		}
	})
}

alex.vv 10.05.2014 22:55

Например, через глобальную переменную.

a.malitsky 11.05.2014 01:25

или замыкание другой функцией, или объект в котором animate - метод

cyber 11.05.2014 02:14

не проверял но должен работать
function animate (opts) {
	var start, move;

     if( animate.state ) return;

     animate.state = true;


	start = new Date();

	move = setInterval(function () {

	var progres = (new Date - start) / opts.progres;

		if(progres >= 1) progres = 1;

		opts.init(progres);
		/* для каждого свою дыстанцию */

		if(progres == 1) {
           animate.state = false;
           clearInterval(move);
         }
	},10)
}

function draw (to, progres, elem) {
	animate({
		to: to,
		progres: progres,
		init: function (progres) {
			elem.style.left = this.to*progres + 'px';
		}
	})
}


Часовой пояс GMT +3, время: 23:04.