setInterval Fail

by Ehsan Ziya

HTML

<input type="range" id="tempo" min="50" max="150" step="1" value="120">

JavaScript

var context = new webkitAudioContext();
var tempo = 120;
var secondsPerBeat = 60.0 / tempo;
var timeinMS = secondsPerBeat * 1000;
//now = context.currentTime;
var test = function(){
var now = context.currentTime;
    var osc = context.createOscillator();
    osc.connect(context.destination);
    osc.start(0);
    osc.start(now);
    osc.stop(now + 0.2);
    //console.log(now);
};

$('#tempo').change(function(){
    tempo = $(this).val();
    secondsPerBeat = 60.0 / tempo;
    timeinMS = secondsPerBeat * 1000;
    window.clearInterval(id);
    var id = setInterval(test,timeinMS);
    
})
//var id = setInterval(test,timeinMS);
//window.clearInterval(id);