Why isn't the setInterval disabling happening?

  • 0
    I can't stop setInterval when amount == 10, for some reason clearInterval (1) doesn't work, what could be the matter?
    let leftOff = 0,
    	topOff = 0,
    	amount = 0,
    	speed = 200;
    
    function move(){
    	if(leftOff < 200 && topOff <= 0){
    		$('#one').offset({
    			left: leftOff++
    		});
    	}else if(leftOff >= 200 && topOff < 200){
    		$('#one').offset({
    			top: topOff++
    		});
    	}else if(leftOff > 0 && topOff >= 200){
    		$('#one').offset({
    			left: leftOff--
    		});
    	}else if(leftOff <= 0 && topOff > 0){
    		$('#one').offset({
    			top: topOff--
    		});
    	}
    }
    
    setInterval(move, speed);
    
    $('#one').click(function(event){
    	if(amount === 10){
    		clearInterval(1);
    		$('#one').text('Вы выиграли');
    		return;
    	}else{
    		speed -= 10;
    		amount++;
    		clearInterval(1);
    		setInterval(move, speed);
    		$('#one').text(amount);
    	}
    });
    JavaScript Harper Sharp, Aug 18, 2019

  • 1 Answers
  • 0
    clearInterval (1);

    Is this some kind of joke? Where did you find such a solution to the problem?

    clearInterval



    let leftOff = 0,
    topOff = 0,
    amount = 0,
    speed = 200,
    intervalId;

    function move(){
    if(leftOff < 200 && topOff <= 0){
    $('#one').offset({
    left: leftOff++
    });
    }else if(leftOff >= 200 && topOff < 200){
    $('#one').offset({
    top: topOff++
    });
    }else if(leftOff > 0 && topOff >= 200){
    $('#one').offset({
    left: leftOff--
    });
    }else if(leftOff <= 0 && topOff > 0){
    $('#one').offset({
    top: topOff--
    });
    }
    }

    intervalId = setInterval(move, speed);

    $('#one').click(function(event){
    if(amount === 10){
    clearInterval(intervalId);
    $('#one').text('Вы выиграли');
    return;
    }else{
    speed -= 10;
    amount++;
    clearInterval(intervalId);
    intervalId = setInterval(move, speed);
    $('#one').text(amount);
    }
    });
    Eloise Ferrell

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!