How do I call a function just once?

  • 0
    Hello, the code below calls setTimeout as many times as the user has clicked on the element, but the task is to call the function once after complete execution of setTimeout, and not as many times as the user wants. How to do it?

    game_start() {
    		const arr_bet = [];
    
    		for ( let bet of this.bet ) {
    			bet.addEventListener('click', (e) => {
    				const bet_value = e.target;
    
    				arr_bet.push(Number(bet_value.innerText));
    
    				const bet = arr_bet.reduce((sum, current) => {
    					return sum + current;
    				}, 0);
    
    				console.log(bet);
    
    				setTimeout((e) => {
    					this.active[0].classList.add('hidden');
    					this.active[1].classList.add('hidden');
    					
    					console.log('end');
                                            // Здесь по идее должна вызываться функция.
    				}, 10000);
    			})
    		}
    	}
    JavaScript Isabella Kerr, Nov 20, 2019

  • 1 Answers
  • 0
    Make some kind of flag. Which is false by default. On each execution, check this flag, if from still false - execute the function in which the flag is toggled to true
    Anonymous

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