Trying to create a clock that switches between time zones via user input

I am trying to create a clock that switches between time zones via user input that is inputted by a prompt. So far it will only go to eastern and always reverts back to eastern whenever I enter any of the other numbers. Any help would be appreciated! Here is my code:

var a = prompt('What is your time zone? Eastern time zone = 1, Central time zone = 2, Mountain time zone = 3, Pacific time zone = 4');


switch (a) {
        case '1':
        setInterval(eastern, 1000);
        clearInterval();

        case '2':
        setInterval(central, 1000);
        clearInterval();

        case '3':
        setInterval(mountain, 1000);
        clearInterval();
        
        case '4':
        setInterval(pacific, 1000);
        clearInterval();
}


setInterval(eastern, 1000)
     function eastern() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    eastern()
    clearInterval()


setInterval(central, 1000); 
     function central() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour - 1 + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    central()
    clearInterval()

    setInterval(mountain, 1000); 
     function mountain() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour - 2 + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    mountain()
    clearInterval()

    setInterval(pacific, 1000); 
     function pacific() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour - 3 + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    pacific()
    clearInterval()
1 Like

Hello and welcome to the freeCodeCamp community.

Briefly, let’s review the setInterval and clearInterval syntax.

setInterval takes two arguments. The first is the callback function to invoke each time the interval triggers, and the second is the time between executions (in milliseconds).

First, let’s define a callback function.

function callback() {
  console.log("I've been called!");
}

Now let’s set an interval to fire every second.

setInterval(callback, 1000);

This will call the callback function every second. However, we have no way to stop it at this time.

We can stop intervals with the clearInterval function. This function takes one parameter, which is the interval to clear. In order to use this, then, we need to assign our setInterval to a variable.

const interval = setInterval(callback, 1000);

Now we can clear it.

clearInterval(interval);

Hope this helps you move in the right direction. :slight_smile:

2 Likes

Thank you for replying so quickly! I tried your suggestions, but unfortunately they didn’t really seem to change much. Here is my updated code:

var a = prompt('What is your time zone? Eastern time zone = 1, Central time zone = 2, Mountain time zone = 3, Pacific time zone = 4');


switch (a) {
        case '1':
        setInterval(eastern, 1000);
        clearInterval(eastern);

        case '2':
        setInterval(central, 1000);
        clearInterval(central);

        case '3':
        setInterval(mountain, 1000);
        clearInterval(mountain);
        
        case '4':
        setInterval(pacific, 1000);
        clearInterval(pacific);
}

const interval1 = setInterval(eastern, 1000);
const interval2 = setInterval(central, 1000);
const interval3 = setInterval(mountain, 1000);
const interval4 = setInterval(pacific, 1000);

setInterval(eastern, 1000)
     function eastern() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    eastern()
    

setInterval(central, 1000); 
     function central() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour - 1 + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    central()
    
    setInterval(mountain, 1000); 
     function mountain() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour - 2 + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    mountain()
    
    setInterval(pacific, 1000); 
     function pacific() { 
        let time = new Date(); 
        let hour = time.getHours(); 
        let min = time.getMinutes(); 
        let sec = time.getSeconds(); 
    
        
        hour = hour < 10 ? "0" + hour : hour; 
        min = min < 10 ? "0" + min : min; 
        sec = sec < 10 ? "0" + sec : sec; 
    
        let currentTime = hour - 3 + ":" 
                + min + ":" + sec; 
    
        document.getElementById("clock") 
                .innerHTML = currentTime; 
    } 
    pacific()
    
1 Like
  • You’re not assigning your intervals to variables. clearInterval takes an interval variable, not a function callback.

  • You’re also trying to clear the interval you set immediately after you set it.

1 Like