如何在调用事件后延迟函数的执行
How do I delay a function from executing after an event has been called?
我有一个游戏的旋转器。我想一个问题形式出现一旦旋转器已停止旋转。是否有一种方法可以延迟启动后点击旋转的形式?
/*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 + 1)) + 1;
totalDegree = newDegree + extraDegree;
/*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;
$("#txt").html(aoY);
console.log(aoY);
/*23.7 is the minimum 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;
});
//have form appear once the spinner stops
showForm();
});
function showForm() {
if (aoY > 0) {
$("#formWrapper").show();
};
}
不是很确定在这段代码是什么停止旋转,但我看到一个removeClass("spin")在setTimeout函数。为什么不直接在这个函数中触发表单呢?
$('#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 + 1)) + 1;
totalDegree = newDegree + extraDegree;
/*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;
$("#txt").html(aoY);
console.log(aoY);
/*23.7 is the minimum 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');
showForm();
}, 100);
}
}, 10);
$('#inner-wheel').css({
'transform': 'rotate(' + totalDegree + 'deg)'
});
noY = t.offset().top;
});
//have form appear once the spinner stops
});
function showForm() {
if (aoY > 0) {
$("#formWrapper").show();
};
}
新建议
看起来你正在使用css变换/动画来动画轮子的旋转。为什么不利用jQuery动画来做动画呢?然后,您可以点击jQuery animate的完整方法,在车轮停止旋转后执行一些操作。
$('#inner-wheel').animate({ textIndent: 0 }, {
step: function( now, fx ) {
$(this).css('transform', 'rotate(' + totalDegree + 'deg)' );
},
complete: function() {
setTimeout( function() {
showForm( 1 )
}, 5000);
}
});
相关文章:
- 如何延迟函数
- 如何在 JS 中调用 2 次或更多次时延迟函数执行
- jQuery/Javascript 延迟函数的执行,直到多个函数完成
- 如何按顺序调用延迟函数
- 使用promise或setTimeout确定延迟函数的调用顺序
- 如何在 QML 中创建延迟函数
- jQuery deferred:用于延迟函数的返回,直到函数内的异步调用完成+获取返回值
- 如何在 AJAX 函数中添加延迟函数
- 节点 js 从客户端和另一个节点模块调用相同的延迟函数
- 如何对延迟函数执行单元测试
- 延迟函数执行(API 调用)以在时间段内执行 n 次
- 延迟函数删除类
- 链接延迟函数
- js,延迟函数
- RxJS 和 React 的 setState - 延迟函数执行直到订阅
- Javascript 延迟函数,但让循环等待它
- 如何延迟函数以使其完全加载
- Javascript延迟函数
- 试图理解延迟函数的语法
- 在流畅的Javascript API中延迟函数的返回