【问题标题】:run function after eventlistener finished事件监听器完成后运行函数
【发布时间】:2018-01-12 00:59:41
【问题描述】:

我有以下代码来检查是否有陀螺仪可供用户交互。我通过以下方式执行此操作:

function check_user_hardware(){
    window.addEventListener("devicemotion", function(event){
        if(event.rotationRate.alpha || event.rotationRate.beta || event.rotationRate.gamma){
            if (!gyroscope) {
                gyroscope = true;
                current_interaction_mode = 'gyroscope_option';
                set_user_ui_elements();
            }
        }else{
            followMouse = true;
            current_interaction_mode = 'followMouse_option';
            console.log("checked for motion");
            set_user_ui_elements();
            window.addEventListener('mousemove', get_user_mouse_pos);
        }
        calculate_rotation_mesh_pos(event.rotationRate.beta, event.rotationRate.gamma);
    }, function(){
        console.log("generate_scene???");
        generate_scene();
    });
}

我遇到的问题是在此检查之后生成了一些场景。
但是这个场景需要首先在检查中设置一个 var。但是检查需要很长时间,所以我在检查完成后添加了一个回调。
但是这个回调永远不会触发......为什么?换句话说,generate_scene??? 永远不会被记录,generate_scene(); 永远不会运行。

为什么会这样?什么是正确的方法?

如果有什么不清楚的地方请告诉我,以便我澄清:)

【问题讨论】:

    标签: javascript jquery callback three.js


    【解决方案1】:

    你的问题是下一个:你的 .addEventListener 方法中的第三个参数是函数,它不应该是函数,因为在文档中第三个参数是布尔值,如果恰好是“useCapture”。 尝试从 check_user_hardware() 中声明 generate_scene 函数,然后在之后调用它

    calculate_rotation_mesh_pos(event.rotationRate.beta,event.rotationRate.gamma);
    generate_scene();
    

    它可能会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-30
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多