AngularJS:跟踪在Web应用程序中花费的时间,然后在特定时间触发事件

AngularJS: Track the time spent in the web app then trigger an event on specific time

本文关键字:然后 定时间 事件 时间 Web 跟踪 应用程序 AngularJS      更新时间:2023-09-26

我想跟踪用户使用网站的时间,然后我需要在用户在网站上停留 30 秒时引发事件。关于如何做到这一点的任何想法?

为什么不使用 Angular 内置的 $timeout

$timeout(function raiseEvent(){
   //do something here
}, 30000)

代码在哪里?

这里有一个没有 Angular 代码的示例,但如果您希望它进行藻化,请随意使用$timeout并将其放入您的 app.run 中。

// This event starts watching when document loads
// Any special event can work to trigger this instead
$(document).ready(function(){
    var countdown = 30000;
    // 30 Second  Event
    function thirty_second_event() {
        console.log("it's been " + (countdown/1000) + " seconds");
    }
    // Start the timer
    setTimeout( thirty_second_event, countdown );
});