Tôi muốn tạo bộ đếm thời gian bằng JavaScript, tôi sẽ thiết lập 10 giây và yêu cầu người dùng nhấp vào hình ảnh và hiển thị số lượng khi thời gian dừng lại.Làm thế nào để tạo bộ đếm thời gian trong javascript và đếm số nhấp chuột trong thời gian được chỉ định?
Làm cách nào để tôi có thể thực hiện tác vụ này?
index.html
<div class="container">
<div class="row">
<div class="col-md-2">
<img src="https://images.omkt.co/Files/402/15EAC0/19F78/8c2112737cfe4cd88779712a4906ef6d/0/hero_forte_2014--kia-1920x.png" height="150px" width="150px" id="clicks" onClick="clickMe()">
</div>
<div class="col-md-2">
<img src="https://images.omkt.co/Files/402/15EAC0/19F78/8c2112737cfe4cd88779712a4906ef6d/0/hero_forte_2014--kia-1920x.png" height="150px" width="150px" id="clicks" onClick="clickMe()">
</div>
<div class="col-md-2">
<img src="https://images.omkt.co/Files/402/15EAC0/19F78/8c2112737cfe4cd88779712a4906ef6d/0/hero_forte_2014--kia-1920x.png" height="150px" width="150px" id="clicks" onClick="clickMe()">
</div>
</div>
<div>
<p id="clicks">0</p>
</div>
index.js
var clicks = 0;
function clickMe() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
};
var digit=-1;
var min=0;
var time;
var timer_is_on=0;
function timer(){
digit++;
if(digit>59){
min++;
document.getElementById("mins").innerHTML=min;
digit=0;
}
// Put a "0" at the start of seconds
if (digit <= 9)
digit = '0' + digit;
document.getElementById("secs").innerHTML=digit;
}
Trước hết, bạn nên biết rằng id phải là duy nhất. Nếu không có tài liệu. sẽ tìm thấy một yếu tố sai. –