Angular 2如何使用Observables在setInterval上获取mouseX?

时间:2017-03-05 10:54:36

标签: angular angular2-observables

我在setInterval中获取当前鼠标位置。如何使用Typescript和Angular 2完成相同的操作?

我的意思是我可以使用Observables创建一个计时器,但是如何读取该计时器中的当前鼠标位置?

let startX = event.pageX;

setInterval(function(){
    let diff = window.mouseX - startX;
    console.log(diff);
}, 50)

1 个答案:

答案 0 :(得分:0)

你可以这样做:

    let startX = event.pageX;


   let observable =  Observable.create( (observer: any) => {

        setInterval(function(){
          let diff = window.mouseX - startX;
          observer.next(diff);
       }, 50)

    });