Why is my interval only cleared halfway?

I am working on my first major programming project, and so far everything is going well. My goal is to run the animation until an event occurs or 15 seconds pass. After the animation ends, I want the animation to repeat. My current plan decision is shown below.

    var animationSpeed = 40;
    var animationTimout = 375;
    var testTime = 1;
    //some more set up variables

    function onClick() {
        startDrive();
        setTimeout(startDrive, 15000); //****
    }

    function startDrive() {
        var Driving = setInterval(carDriving, aniSpeed);
    }

    function carDriving() {
        testLocation();
        drawCar();
        angleCalc();
        newLocation();
        getInfo();
    }

    function testLocation() {
        //this code gets information on whether or not the animation should be stopped
        testTime = testTime + 1

        if(a === 1 || testTime > animationTimeout) {
            //a test to cancel the animation, the other variables test to
            clearInterval(Driving);
        }
    }

    function drawCar() {
        //draws the car
    }

    function angleCalc() {
        //gets some info on how to move the car
    }

   function newLocation() {
        //decides on new coords for the car based on angleCalc();
    }

    function getInfo() {
        //gets some info I plan to use later in the project
    }

When I run the code without a highlighted line, everything works. The car comes to life as I want, and stops if the conditions for stopping are met. The machine freezes where it was on the canvas, and it seems that the interval has been cleared. When I add a highlighted line of code, the animation seems to work, but it works twice as fast as before. I am completely lost and do not try to work anything. Please, help.

+4
1

, , , :

function startDrive() {
    var Driving = setInterval(carDriving, aniSpeed);
}

Driving startDrive, , var . , testLocation(), . , clearInterval(Driving), Driving . Driving , var:

function startDrive() {
    Driving = setInterval(carDriving, aniSpeed);
}

testLocation. .

+3

Source: https://habr.com/ru/post/1623632/


All Articles