Is there a way to make a different function happen after every single click on the document in js?

Im trying to make a gaming website in which when the user first signs in, they are greeted by two show lights that shine down on words that say: “WELCOME TO COSMIC GAMES!” I want to make it interactive, so I’ve been trying to make it so that when the user clicks anywhere on the document, it displays one of the words. I already got the first word, the problem, though is trying to call the second function to light up the second word without calling the first function.

This is what I tried:

var clicks = 0;

function playLightSwitch1() {
  clicks = 1;
  audio  = new Audio("lightSwitch.mp3");
  audio.play();
  document.getElementById("welcomeText").setAttribute("class", "glowWelcome");
  document.getElementById("whiteTraingle1").setAttribute("class", "whiteTraingleVisible1");
  document.getElementById("whiteTraingle2").setAttribute("class", "whiteTraingleVisible2"); 
}

if (clicks == 0) {
  document.addEventListener("click", playLightSwitch1);
}
if (clicks = 1) {
  document.removeEventListener("click", playLightSwitch1);
}

the first function worked, but when I clicked again, the first function ran. I just want nothing to happen on the second click. PS: ignore the setAttribute stuff, that’s just changing the text so it glows.

New contributor

massiveFatty372 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

There are some logical problems in your code.

  • The first problem is that you used = in the condition, but it was wrong.
  • Second, you used if conditions outside the scope of function.

I provide updated code. Hope it helps you.

var clicks = 0;

function playLightSwitch1() {
    if (clicks === 0) {
        clicks = 1;
        var audio = new Audio("lightSwitch.mp3");
        audio.play();
        document.getElementById("welcomeText").setAttribute("class", "glowWelcome");
        document.getElementById("whiteTraingle1").setAttribute("class", "whiteTraingleVisible1");
        document.getElementById("whiteTraingle2").setAttribute("class", "whiteTraingleVisible2");
        
        document.removeEventListener("click", playLightSwitch1);
    }
}

// Add the event listener initially
document.addEventListener("click", playLightSwitch1);

3

Instead of dynamically adding/removing event handlers, a better approach to achieve your goal would be to have a single event handler which iterates through a set of actions on each click.

You can place these actions inside an array and execute each one based on an index which increments as the document (or whatever target element you require) is clicked.

Here’s a working example:

const welcomeDiv = document.querySelector('.welcomeText');
const triangle1Div = document.querySelector('.whiteTriangle1');
const triangle2Div = document.querySelector('.whiteTriangle2');
const audio = new Audio('lightSwitch.mp3');

const actions = [
  () => welcomeDiv.classList.add('glowWelcome'),
  () => triangle1Div.classList.add('whiteTraingleVisible1'),
  () => triangle2Div.classList.add('whiteTraingleVisible2'),
]

const playLightSwitch = () => {  
  /* removed as audio element not in demo
  audio.currentTime = 0;
  audio.play(); 
  */
  
  let clickIndex = document.body.dataset.clickIndex || 0;
  actions[clickIndex % actions.length]();
  document.body.dataset.clickIndex = ++clickIndex;  
}

document.addEventListener('click', playLightSwitch);
body {
  font-size: 2rem;
  font-weight: bold;
}

.glowWelcome {
  color: #C00;
}

.whiteTraingleVisible1 {
  color: #0C0;
}

.whiteTraingleVisible2 {
  color: #00C;
}
<div class="welcomeText">Welcome</div>
<div class="whiteTriangle1">Triangle1</div>
<div class="whiteTriangle2">Triangle2</div>

A few things to note in this implementation:

  • The use of addEventListener() instead of adding onclick attributes to the DOM.
  • Using classList.add() instead of updating class attributes.
  • Preloading the single audio element when the DOM loads, instead of re-creating it every time the event fires.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật