Javascript 单击按钮后才开始的时间
是否只有在给定的JSFIDLE链接中单击一个按钮后才能启动计时器Javascript 单击按钮后才开始的时间,javascript,jquery,html,Javascript,Jquery,Html,是否只有在给定的JSFIDLE链接中单击一个按钮后才能启动计时器 var秒=0; var得分=0; var指数=0; 倒计时(60); 函数倒计时(秒){ 秒=秒; 勾选(); } 函数tick(){ var计数器=document.getElementById(“计时器”); 秒--; counter.innerHTML=“时间:”+字符串(秒); 如果(秒数>60&&0分){ 设置超时(勾号,1000); } } var秒=0; var得分=0; var指数=0; var=false; 函
var秒=0;
var得分=0;
var指数=0;
倒计时(60);
函数倒计时(秒){
秒=秒;
勾选();
}
函数tick(){
var计数器=document.getElementById(“计时器”);
秒--;
counter.innerHTML=“时间:”+字符串(秒);
如果(秒数>60&&0分){
设置超时(勾号,1000);
}
}
var秒=0;
var得分=0;
var指数=0;
var=false;
函数倒计时(秒){
秒=秒;
勾选();
}
函数tick(){
var计数器=document.getElementById(“计时器”);
秒--;
counter.innerHTML=“时间:”+字符串(秒);
如果(秒数>60&&0分){
设置超时(勾号,1000);
}
}
$(“按钮”)。单击(函数(){
如果(!已启动){
开始=真;
倒计时(60);
}
});
您可以在单击后生成自定义事件
$(window.trigger('clicked')代码>
请记住检查倒计时是否尚未开始
$("button").click(function(){
});
这将为DOM中的所有按钮添加一个处理程序,然后检查倒计时的状态
检查这把小提琴:
在单击处理程序中有一个函数和一个变量,用于说明函数是否已启动
您还可以将单击事件合并到单个处理程序中,并且索引可以作为data-*属性的一部分存储,从而减少重复代码
Javascript
//Timer //
var seconds = 0,
Score = 0,
index = 0,
timerStarted = false;
function countdown(sec) {
seconds = sec;
tick();
}
function tick() {
var counter = document.getElementById("timer");
seconds--;
counter.innerHTML = "Time : " + String(seconds);
if (seconds > 60 && Score < 30) {
alert("Not enough Score");
}
if (seconds > 0) {
setTimeout(tick, 1000);
}
}
$("#one, #two, #three").click(function () {
if(!timerStarted) {
countdown(60);
timerStarted = true;
}
var dataIndex = $(this).data('index');
if (index == dataIndex) {
Score++;
if(dataIndex == 2) {
index = 0;
} else {
index++;
}
}
$("#score").html("Score: " + Score);
});
//计时器//
var秒=0,
分数=0,
指数=0,
timerStarted=false;
函数倒计时(秒){
秒=秒;
勾选();
}
函数tick(){
var计数器=document.getElementById(“计时器”);
秒--;
counter.innerHTML=“时间:”+字符串(秒);
如果(秒数>60且分数<30){
警惕(“分数不够”);
}
如果(秒>0){
设置超时(勾号,1000);
}
}
$(“一、二、三”)。单击(函数(){
如果(!timerStarted){
倒计时(60);
timerStarted=true;
}
var dataIndex=$(this.data('index');
如果(索引==数据索引){
分数++;
如果(数据索引==2){
指数=0;
}否则{
索引++;
}
}
$(“#分数”).html(“分数:+score”);
});
HTML
<div id="timer"></div>
<div id="score">Score: 0</div>
<button id="one" type="button" data-index="0">Button1</button>
<button id="two" type="button" data-index="2">Button2</button>
<button id="three" type="button" data-index="1">Button3</button>
分数:0
按钮1
按钮2
按钮3
很抱歉,计时器没有运行,并且您提供的JSFIDLE链接似乎没有任何工作……单击按钮,等待一秒钟,秒数减少一秒。还有一个错误,试试为什么计时器没有显示在第一页,如果我把相同的代码放在两页。。。
$("button").click(function(){
});
//Timer //
var seconds = 0,
Score = 0,
index = 0,
timerStarted = false;
function countdown(sec) {
seconds = sec;
tick();
}
function tick() {
var counter = document.getElementById("timer");
seconds--;
counter.innerHTML = "Time : " + String(seconds);
if (seconds > 60 && Score < 30) {
alert("Not enough Score");
}
if (seconds > 0) {
setTimeout(tick, 1000);
}
}
$("#one, #two, #three").click(function () {
if(!timerStarted) {
countdown(60);
timerStarted = true;
}
var dataIndex = $(this).data('index');
if (index == dataIndex) {
Score++;
if(dataIndex == 2) {
index = 0;
} else {
index++;
}
}
$("#score").html("Score: " + Score);
});
<div id="timer"></div>
<div id="score">Score: 0</div>
<button id="one" type="button" data-index="0">Button1</button>
<button id="two" type="button" data-index="2">Button2</button>
<button id="three" type="button" data-index="1">Button3</button>