Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angularjs Onbeforeunload在没有提示的情况下工作不正常_Javascript_Angularjs_Onbeforeunload - Fatal编程技术网

Javascript Angularjs Onbeforeunload在没有提示的情况下工作不正常

Javascript Angularjs Onbeforeunload在没有提示的情况下工作不正常,javascript,angularjs,onbeforeunload,Javascript,Angularjs,Onbeforeunload,我想在窗口关闭前执行一些代码,但不想显示提示。 这是我的密码: $window.onbeforeunload = function(){ $rootScope.$broadcast('war-change'); return undefined; }; $scope.$on('war-change',function(){ console.log('here'); //Execute some more co

我想在窗口关闭前执行一些代码,但不想显示提示。 这是我的密码:

  $window.onbeforeunload = function(){
        $rootScope.$broadcast('war-change');
        return undefined;
    };

    $scope.$on('war-change',function(){
        console.log('here');
         //Execute some more code here.
        });
返回undefined或false不起作用,因为我的代码没有得到执行。
非常感谢您的帮助。

您的代码确实按预期工作。检查以下小提琴:

但是,使用beforeunload方法可以做的事情有一些限制,因为一旦提交了unload,您就有非常有限的时间来做一些事情。要测试您有多少时间,可以使用以下修改:

$scope.$on('war-change',function(){
    console.log('here');
     //Execute some more code here.
     setTimeout(function(){ console.log("ha-ha")}, 100)
     console.log("he-he")
    });

现在,如果您将超时减少到0-100,您仍然可以在输出中看到“ha”。增加到2秒(2000),你很可能看不到那一行

您的代码确实按预期工作。检查以下小提琴:

但是,使用beforeunload方法可以做的事情有一些限制,因为一旦提交了unload,您就有非常有限的时间来做一些事情。要测试您有多少时间,可以使用以下修改:

$scope.$on('war-change',function(){
    console.log('here');
     //Execute some more code here.
     setTimeout(function(){ console.log("ha-ha")}, 100)
     console.log("he-he")
    });
现在,如果您将超时减少到0-100,您仍然可以在输出中看到“ha”。增加到2秒(2000),你很可能看不到那一行

$scope.$on('war-change',function(){
    console.log('here');
     //Execute some more code here.
     setTimeout(function(){ console.log("ha-ha")}, 100)
     console.log("he-he")
    });