Javascript 禁用。首次使用后单击事件
我正在尝试修改此js代码以禁用。单击按钮后单击事件。我是js的初学者,对js只有基本的了解。我曾尝试使用if语句,但没有成功 以下是js代码:Javascript 禁用。首次使用后单击事件,javascript,jquery,Javascript,Jquery,我正在尝试修改此js代码以禁用。单击按钮后单击事件。我是js的初学者,对js只有基本的了解。我曾尝试使用if语句,但没有成功 以下是js代码: var sec = ["30% ENTER KEYCODE: Thirty", "25% ENTER KEYCODE: Twenty-five", "20% ENTER KEYCODE: Twenty","15% ENTER KEYCOVE: Fifteen","10% ENTER KEYCODE: Ten","5% EMTER KEYCODE: Fiv
var sec = ["30% ENTER KEYCODE: Thirty", "25% ENTER KEYCODE: Twenty-five", "20% ENTER KEYCODE: Twenty","15% ENTER KEYCOVE: Fifteen","10% ENTER KEYCODE: Ten","5% EMTER KEYCODE: Five"];
var offset = 30;
//set default degree (360*5)
var degree = 1800;
//number of clicks = 0
var clicks = 0;
$(document).ready(function(){
/*WHEEL SPIN FUNCTION*/
$('#spin').click(function(){
//add 1 every click
clicks ++;
/*multiply the degree by number of clicks
generate random number between 1 - 360,
then add to the new degree*/
var newDegree = degree*clicks;
var extraDegree = Math.floor(Math.random() * 360) + 1;
totalDegree = newDegree+extraDegree;
var colorIndex = Math.ceil(((totalDegree+30) % 360) / 60) -1;
var colorPrev = colorIndex == 0 ? 5 : colorIndex - 1;
var colorNext = colorIndex == 5 ? 0 : colorIndex + 1;
offset = (extraDegree % 60);
var result = sec[colorIndex];
if(offset == 0) {
result ;
} else if (offset <= 30) {
result ;
} else {
result ;
}
$("#answer").html("Answer: " + result);
/*let's make the spin btn to tilt every
time the edge of the section hits
the indicator*/
$('#wheel .sec').each(function(){
var t = $(this);
var noY = 0;
var c = 0;
var n = 700;
var interval = setInterval(function () {
c++;
if (c === n) {
clearInterval(interval);
}
var aoY = t.offset().top;
console.log(aoY);
/*23.7 is the minumum offset number that
each section can get, in a 30 angle degree.
So, if the offset reaches 23.7, then we know
that it has a 30 degree angle and therefore,
exactly aligned with the spin btn*/
if(aoY < 23.89){
console.log('<<<<<<<<');
$('#spin').addClass('spin');
setTimeout(function () {
$('#spin').removeClass('spin');
}, 100);
}
}, 10);
$('#inner-wheel').css({
'transform' : 'rotate(' + totalDegree + 'deg)'
});
noY = t.offset().top;
});
});
});//DOCUMENT READY
var sec=[“30%输入键码:30”,“25%输入键码:25”,“20%输入键码:20”,“15%输入键码:15”,“10%输入键码:10”,“5%输入键码:5”];
var偏移=30;
//设置默认角度(360*5)
var度=1800;
//点击次数=0
var=0;
$(文档).ready(函数(){
/*车轮自旋函数*/
$('#spin')。单击(函数(){
//每单击一次添加1
点击++;
/*将次数乘以单击次数
生成1-360之间的随机数,
然后添加到新的学位*/
var newDegree=点击次数*degree;
var extraDegree=Math.floor(Math.random()*360)+1;
totalDegree=新度+额外度;
var colorIndex=Math.ceil((总度数+30)%360)/60)-1;
var colorPrev=colorIndex==0?5:colorIndex-1;
var colorNext=colorIndex==5?0:colorIndex+1;
偏移量=(超度数%60);
var结果=秒[颜色指数];
如果(偏移量==0){
结果;
}else if(offset如果您只想在第一次单击后删除侦听器的one()
方法后使用它
更改:
$('#spin').click(function(){
到
$('#spin')。单击(函数(){
点击++;
如果(单击次数<1){
//其他人编码和/或重置变量
}
}
另外,将单击更改为$(“#spin”)。在('click',function(){}
上,其他主持人给出了一些提示。无论如何,如果有帮助,您可以尝试其中任何一种
使用disabled
属性的简单解决方案非常方便
$(函数(){
$('#spin')。单击(函数(){
console.log('clicked..',这个);
$(this.prop('disabled',true);
});
});
自旋
禁用多长时间?永远?还是以后要重新初始化它?将(“#自旋”)置于('click')关闭('click'));
在click-handler内部。目标是旋转轮子一次,然后禁用按钮,因此无法继续单击按钮并旋转轮子如果您只想在旋转过程中禁用按钮,请不要使用.one()
或.off()
,它将删除整个单击处理程序。相反,在单击处理程序中放入类似的内容:如果(!$(this.hasClass('spining')){$(this.addClass('spining');…其余的代码…}
。然后当控制盘停止旋转时(或在适当的时候),使用$('spin')。removeClass('spining'));
再次删除该类。这将有效地禁用该按钮,但会给您更多的控制权以再次使用它。应该可以正常工作。浏览器控制台中有错误吗?jQuery的哪个版本?
$('#spin').one('click',function(){
$('#spin').click(function(){
clicks ++;
if (clicks < 1 ) {
//rest of You code and/or reset clicks variable
}
}
var clicks = 0;
$(function () {
$('body').on('click', '#spin', function () {
//
if (clicks > 0) {
return false;
}
// write code here for first click
clicks++;
})
})