为什么函数不显示参数值?

问题描述 投票:0回答:1

我是新手,我正在帮助一位同事调试代码。下面的代码不会将width和height值返回给show_camera函数。在控制台中,show cam width始终显示0

function show_camera($, width, height) {
    $('#snap').css('display', 'block');
    $('#camera-container').css('display', 'block');

    $('#avatar img').css('width', width);
    console.log("show cam width: " + width);

}

下面是上面的函数的调用方式。

jQuery(document).ready(function($) {
    center_buttons($);

    $(document).on('click', '#cam-button', function(e) {
        e.preventDefault();
        var video = document.getElementById('video');

        const vgaConstraints = {
                video: {
                    width: { min: 800, ideal: 800, max: 1280 },
                    height: { min: 450, ideal: 450, max: 720 }
                }
        };
        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
            navigator.mediaDevices.getUserMedia(vgaConstraints).then(function(stream) {
                    localstream = stream;
                    FLAG = true;
                    video.srcObject = stream;
                    video.play();
                    show_camera($, video.getBoundingClientRect().width, video.getBoundingClientRect().height);
                    console.log("width:" + video.getBoundingClientRect().width);
                    console.log("height:" + video.getBoundingClientRect().height);
                })
                .catch(function(err) {
                    //alert(AJAX_OBJ.perm_txt);
                    alert(err.name + ": " + err.message);
                    console.log(err.message);
                    hide_camera($);
                });
...
...

我可以从上面获得width: 280。但是为什么不使用show_camera函数。

javascript jquery
1个回答
0
投票

因此,您尝试用来获取函数的width属性的代码由于以下两个原因而无法正常工作:使用.width正在查找从函数返回的JSON值(有关更多信息,请参见here),并且因为您没有从函数返回任何值(即,您没有授予代码读取函数中变量的权限)。

尝试此:

function show_camera($, width, height) {
   $('#snap').css('display', 'block');
   $('#camera-container').css('display', 'block');

       $('#avatar img').css('width', width);
       console.log("show cam width: " + width);
   return { "width" : width , "height" : height}
}

再次,可以找到关于JSON对象的更多信息here

© www.soinside.com 2019 - 2024. All rights reserved.