0

I created a service to count time, when I click start time starts and only stops until it is clicked the pause button.

If you have the start time is there any way to close the page or turn off the computer to execute the pauseTimer () function?

Can anyone help me? Tried to use hostlistener but no effect :(

html

<dx-data-grid class="tabelaTask"
    [dataSource]="data"  
    height="600"
    showBorders="true">
      <dxo-paging [pageSize]="3"></dxo-paging>
    <dxo-scrolling mode="single"></dxo-scrolling>
    <dxi-column [width]="100" dataField="Name" caption="Customer"></dxi-column>
    <dxi-column [width]="300" [allowFiltering]="false" [allowSorting]="false" cellTemplate="cellTemplate"></dxi-column>
     <div *dxTemplate="let data of 'cellTemplate'">

  <div class="btn-group" dropdown style="position: absolute;">
    <button id="button-basic" dropdownToggle type="button" class="btn btn-primary dropdown-toggle"
            aria-controls="dropdown-basic">
      Button dropdown <span class="caret"></span>
    </button>
</dx-data-grid>
Mike Landers
  • 440
  • 5
  • 14

1 Answers1

3

This is not possible. As already explained in this post, there is no event that fires when the webpage closes.

You can, however, prevent your user from closing the tab by using the onBeforeUnload function. A detailed explanation on that can be found here.

Alternative approach

You can, however, approach your problem in a different way. If you save the time a timer has started and delete that start time once the user clicks pause, you can detect if you have a "running" timer on your next startup. Additonally, you need a save a starting value, which is basically the starting offset for your timer. Here is a working StackBlitz of that idea.

Update: Applying this to your example

Using the Stackblitz I got from you in chat, I applied this logic to your example. Here is the working StackBlitz.

pascalpuetz
  • 5,238
  • 1
  • 13
  • 26
  • Seriously ? :( so if a pc shuts down abruptly, it is impossible to save the current time :( – Mike Landers Dec 28 '19 at 15:22
  • Exactly. The only thing you can do is save the time in a defined interval. Or do this server side. – pascalpuetz Dec 28 '19 at 15:24
  • Sorry for the inconvenience, but is there a way to have a service always running in the background? That is, if you start the start, the person can close the page that is always counting the time – Mike Landers Dec 28 '19 at 15:27
  • This is only partially possible on mobile devices. On Desktop devices it is not possible. Service Worker would get closest to this but it would be too unreliable for you use case. The best bet would be to count on server side or save the value periodically. Maybe only save the start/end time and calculate the time? This way you only need to know if it has stopped and don't need to update periodically but only when there is a "start" saved but no "end"? – pascalpuetz Dec 28 '19 at 15:31
  • @MikeLanders I updated my answer and created a StackBlitz showcasing this idea. – pascalpuetz Dec 28 '19 at 16:26
  • Thank you so much for trying to help me. What I want to do is save the time when I start the timer and when I stop it. When start is saved in bd the start and give me the ID of that line for when to pause the timer, it goes in that line ID and sets it to the end time. So I know when he started and stopped the timer – Mike Landers Dec 28 '19 at 16:29
  • I think you got it right in what I want, the problem now is to apply it correctly in my example, thank you very much! – Mike Landers Dec 28 '19 at 16:32
  • Oh okay, in that case you have unfortunately no way to do that, except tracking the timer on server side and sending keep-alives... That is, if you want to stop the timer entirely when the page is closed without tracking the time in between the shutdown / back to page. – pascalpuetz Dec 28 '19 at 16:32
  • I thoroughly analyzed and tested and this solution is fantastic, just do not know if my example will work correctly – Mike Landers Dec 28 '19 at 16:34
  • Glad if it helped! :) If you stumble upon problems applying this to your solution, feel free to ask for help again! :) – pascalpuetz Dec 28 '19 at 16:36
  • I am trying to implement, but without success, if you can help me, I am very grateful, but you have time! – Mike Landers Dec 28 '19 at 16:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204990/discussion-between-pascalpuetz-and-mike-landers). – pascalpuetz Dec 28 '19 at 16:49
  • @MikeLanders I updated my answer to include the result from our chat – pascalpuetz Dec 28 '19 at 18:52