javascript - 如何使用 AngularJS 每 3 秒跟踪一次鼠标位置?

标签 javascript angularjs mean-stack

我正在开发 MEAN 堆栈应用程序,我需要每 3 秒跟踪一次鼠标位置,将该数据保存在数组中,然后将其存储到我的数据库中。

我从这个开始,但我不知道如何完成它:

HTML

<div class="field" ng-mousemove="captureCoordinate($event)">

My view is inside here

</div>

JavaScript


//I need to save this into my database
const data = {
      minutes,
      seconds,
      playerId,
      level,
      clicks,
      objectsToFind,
      tracking[]       <--- this should be the array with the tracking info
    }


//This is what I tried to get the mouse coordinates from my HTML
$scope.captureCoordinate = function($event){
      $scope.x = $event.x;
      $scope.y = $event.y;
   }

最佳答案

您可以在鼠标移动时将坐标存储在局部变量中,然后使用该局部变量的值每 3 秒更新一次作用域变量。

类似于:

var app = angular.module("myApp", []);

app.controller('myApp', ['$scope', function($scope) {
  const data = {
    tracking: []
  }
  let coords;
  $scope.trackCoords = function($event) {
    coords = [$event.pageX, $event.pageY];
  };
  setInterval(function() {
    data.tracking = coords;
    console.log(data.tracking);
  }, 3000);


}]);
.field {
  margin: 2%;
  height: 500px;
  border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
<div ng-controller="myApp" ng-app="myApp" class="field" ng-mousemove="trackCoords($event)">
  My view is inside here
</div>

关于javascript - 如何使用 AngularJS 每 3 秒跟踪一次鼠标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58071551/

相关文章:

javascript - 使用 .innerHTML 向 JS 函数发送参数

JavaScript Closure 错误行为

javascript - 单击时如何使用angular js的$location.hash更改html元素的颜色?

javascript - 切换到 angular-ui-router 会导致 $http 出现 500 错误

javascript - 文件无法复制到另一个目录(fs.access 和 copyFile)

javascript - 如何通过在 React 中遍历数组来呈现多个按钮

javascript - Javascript 是如何获取这个值的呢?

javascript - 在 AngularJs 中,如何将函数合并为通用函数以用于不同的 Controller ?

node.js - Mongoose Stream,如何更新每个文档?

node.js - 当我尝试运行此代码时出现以下错误