S
S
sanke462018-02-07 18:02:22
JavaScript
sanke46, 2018-02-07 18:02:22

SetInterval() doesn't stop, why?

When you click on, it should speed up and change the text with the number of hits and close after 10 hits. For some reason it does not accelerate, but everything else is done well :-)

let topOffset = 0;
            let rightOffset = 0;
            let bottomOffset = 200;
            let leftOffset = 200;
            let speed = 10;
            let interval = setInterval(squareCreator, speed);
            let onOrOff = true;
            let tryCounter = 0;


            function squareCreator () {
              realisation(topOffset,rightOffset,bottomOffset,leftOffset);
            };
       /*Тут типа код
       Цикл, который считает попытки, выводит количество попыток, ускоряет перемещение заголовка */
            $('html').click(function () {
                 if(tryCounter == 10) {
                   clearInterval(interval);
                 } else {

                   tryCounter++;
                   speed = speed/2;
                   $('#heading').text('Hello, world!' + ' ' + tryCounter);
                 }
            });

/*============ methods ========== */

        function realisation(one,two,three,four) {
            if(one < 200){
                topSide();
            } else if (two < 200){
                rightSide();
            } else if (three > 0) {
                bottomSide();
            } else if (four > 0) {
                leftSide();
            } else {
                update();
            }
        };

        function topSide () {
           let top = $('#heading').offset ({
            left: topOffset });
            topOffset++;

            console.log("top")
        };

        function rightSide () {
            let right = $('#heading').offset ({
                top: rightOffset });
            rightOffset++;

            console.log("right")
        };

        function bottomSide () {
            let bottom = $('#heading').offset ({
                left: bottomOffset });
            bottomOffset--;

            console.log("bottom")
        };

        function leftSide () {
            let left = $('#heading').offset ({
                top: leftOffset });
            leftOffset--;

            console.log("left")
        };

        function update() {
            topOffset = 0;
            rightOffset = 0;
            bottomOffset = 200;
            leftOffset = 200;
        };

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Coder321, 2018-02-07
@Coder321

And why should it accelerate if you initialized it once and that's it? It is no longer possible to change values ​​in an initialized interval.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question