【发布时间】:2015-12-16 16:14:25
【问题描述】:
我有这个 javascript http://jsfiddle.net/ZDsMa/433/ 来选择一个随机数。我把它放在html div上,我想开始用keypress而不是onClick来打乱数字,也用keypress再次停止
var output, started, duration, desired;
// Constants
duration = 5000;
desired = '5';
// Initial setup
output = $('#output');
started = new Date().getTime();
// Animate!
animationTimer = setInterval(function() {
// If the value is what we want, stop animating
// or if the duration has been exceeded, stop animating
if (output.text().trim() === desired || new Date().getTime() - started > duration) {
clearInterval(animationTimer);
} else {
console.log('animating');
// Generate a random string to use for the next animation step
output.text(
''+
Math.floor(Math.random() * 10)+
Math.floor(Math.random() * 10)
);
}
}, 100);
【问题讨论】:
标签: javascript jquery html javascript-events