How to change the script correctly?

  • 0
    There is a script that alternately changes 2 numbers, I found it, and I have been using it for a long time, but now the task is to change 3 numbers in turn, but I can't modify the script, how to do it correctly?
    & lt; a id = "phone" href = "" & gt; & lt; / a & gt;
    document.addEventListener('DOMContentLoaded', function() {
    	var link = document.getElementById('phone');
    	var a = '+38 (095) 123 45 67';
    	 var b = '+38 (098) 123 45 67';
    	link.innerText = b;
    	link.setAttribute('href', 'tel:' + b);
    	setInterval(function() {
    			link.innerText = link.innerText === b ? a : b;
    			link.setAttribute('href', 'tel:' + link.innerText);
    		  }, 2000);
    	});
    JavaScript Anonymous, Sep 4, 2020

  • 2 Answers
  • 0
    You can add as many numbers as you like to numbers, they will spin in a circle.

    document.addEventListener('DOMContentLoaded', () => {
    const link = document.querySelector('#phone');
    let activeIndex = 0;

    const numbers = ['+38 (095) 123 45 67', '+38 (098) 123 45 67', '+38 (111) 111 11 11'];
    setNumber(numbers[activeIndex]);

    setInterval(() => {
    setNumber(numbers[activeIndex]);
    activeIndex = (activeIndex + 1) % numbers.length;
    }, 2000);

    function setNumber(number) {
    link.innerText = number;
    link.setAttribute('href', 'tel:' + number);
    }

    });
    Xavier Gordon

  • 0
    document.addEventListener('DOMContentLoaded', function() {
    let numbers = [
    '+38 (095) 123 45 67',
    '+38 (096) 123 45 67',
    '+38 (097) 123 45 67',
    '+38 (098) 123 45 67',
    '+38 (099) 123 45 67',
    ];

    function getNumber(numbers){
    numbers.push(numbers.shift());

    return numbers[0];
    }

    function setNumber(number){
    let link = document.getElementById('phone');
    link.innerText = number;
    link.setAttribute('href', 'tel:' + number);
    }

    function update(){
    setNumber(getNumber(numbers));
    }

    update();

    setInterval(update, 2000);
    });
    Anonymous

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