受这个SO answer的启发,我有这段代码,它可以工作,但它会弹出并且听起来不像它可以的那样流畅。
$('.btn').each(initDraggable);
function initDraggable() {
var param = {}
param.drag = drag
param.stop = stopper
$(this).draggable(param)
}
var Variables = {}
Variables.Frequency = 0
function drag(myEvent,myUI) {
var Frequency = myUI.position.left + 100
var Duration = 5000
if (Math.abs(Variables.Frequency-Frequency) > 25) {
if (typeof(Variables.osc) === 'undefined') {
} else {
Variables.osc.stop(0)
}
Variables.Frequency = Frequency
Oscillator(Frequency,Duration)
}
}
function stopper() {
Variables.osc.stop(0)
}
Variables.ctx = new(window.audioContext || window.webkitAudioContext)
function Oscillator(argFrequency,argDuration) {
Variables.osc = Variables.ctx.createOscillator()
Variables.osc.type = 0
Variables.osc.connect(Variables.ctx.destination)
Variables.osc.frequency.value = argFrequency
Variables.osc.start(0)
setTimeout(myTimeout,argDuration)
function myTimeout() {
Variables.osc.stop(0)
}
}
<link rel="stylesheet" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.css">
<span class="btn btn-primary">Drag me</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/bootstrap/latest/js/bootstrap.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui/latest/jquery-ui.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.ui.touch-punch/latest/jquery.ui.touch-punch.js"></script>
问:有没有一种简单的方法可以让这听起来更流畅一些?