当console.log给出正确的值时,为什么返回给未定义?

时间:2018-11-29 18:40:28

标签: javascript return console.log

我遇到了一些JavaScript无法正常使用的问题。我试图以各种方式对其进行调试,但仍然无法以所需的方式工作。我会让代码说明一切。

var audioKey = function() {
    if (context = new (window.OfflineAudioContext || window.webkitOfflineAudioContext) (1, 44100, 44100), !context) {
        return 'unavailable';
    }
    var audio,
        result,
        oscillator = context.createOscillator(),
        compressor = context.createDynamicsCompressor();

    oscillator.type = 'triangle';
    oscillator.frequency.value = 1e4;

    compressor.threshold && (compressor.threshold.value = -50);
    compressor.knee && (compressor.knee.value = 40);
    compressor.ratio && (compressor.ratio.value = 12);
    compressor.reduction && (compressor.reduction.value = -20);
    compressor.attack && (compressor.attack.value = 0);
    compressor.release && (compressor.release.value = .25);

    oscillator.connect(compressor);
    compressor.connect(context.destination);
    oscillator.start(0);
    context.startRendering();

    var audioTimeOut = setTimeout(function() {
        console.warn('Audio fingerprint has timedout.');
        context.oncomplete = function() {}
        context = null;
    }, 1000);

    context.oncomplete = function(event) {
        try {
            clearTimeout(audioTimeOut);
            audio = event.renderedBuffer.getChannelData(0)
                .slice(4500, 5000)
                .reduce(function(acc, val) { return acc + Math.abs(val)}, 0)
                .toSting();
            oscillator.disconnect();
            compressor.disconnect();
        } catch(error) {
            console.log(error);
        }
        result = x64hash128(audio);
        if (result !== null || result !== undefined) {
            console.log(result);
            return result;
        }
    }
}

控制台中的值为:473d1c74db7cd38e1fa15817b1366bfc,但未返回任何内容。当尝试console.log调用时,它返回未定义。尝试和捕获永远不会提供任何东西。我已经移动了代码,使用了变量,并尝试了武器库中的所有技巧。请有人能启发我!谢谢。

0 个答案:

没有答案