JavaScript How to pause listening to the ng Websocket when reloading the page?

I'm using angular ui , I've created a directive that its controller listen to the websockets server and retrieve the data, actually it works fine but when leaving the page that loads the directive the socket client is still listening, my question how to pause listening until the user open the page that loads this directive again then resume listening?

I'm using this module : https://github.com/wilk/ng-websocket

What is the best way to pause listening and resume it? and is it efficient to stop listening and start it again? what is good and bad about this methodology ?

Edit : add some code to clarify

function startWS(host, port, token) {
      ws = $websocket.$new({
          url: 'ws://' + host + ':' + port + '?token=' + token,
          reconnect: true,
          reconnectInterval: 500
      });

      ws.$on('$open', function () {
          console.log('Web Socket Opened');
      });

      ws.$on('$close', function () {
          console.log('Web Socket Closed');
      });

      ws.$on('$message', function (action) {
          console.log(action);
      });
  }

this function is the function used to connect to the websocket, i need some other function to pause this listening and other for resuming.

Answer:1

I am trying to upload pictures without the page refreshing. I used event.preventDefault() in order to stop the page from refreshing. However, the PHP code that is inside of my jQuery code is not being ...

I am trying to upload pictures without the page refreshing. I used event.preventDefault() in order to stop the page from refreshing. However, the PHP code that is inside of my jQuery code is not being ...

I am using eval() to run a script from a string. Below is the code: eval('console.log("hello")'); I will get hello from the console output. I wonder whether I can save the hello into an variable in ...

I am using eval() to run a script from a string. Below is the code: eval('console.log("hello")'); I will get hello from the console output. I wonder whether I can save the hello into an variable in ...

  1. console output from windows service
  2. console output from wpf application
  3. console output from python
  4. console output from java
  5. console output from gradle
  6. console output in c#
  7. console output in javascript
  8. console output in visual studio
  9. console output in jenkins
  10. console output in php
  11. console output in intellij
  12. console output in android studio
  13. console output in visual studio 2017
  14. console output in powershell
  15. console output in typescript
  16. console output in visual studio code
  17. console output in js
  18. console output in jsp
  19. console output in r
  20. console output in html

There is two constructor functions: Animal and Rabbit (classes emulation). How can I set inheritance for this classes using prototypes properly? I seen 2 versions: Rabbit.prototype.__proto__ = Animal....

There is two constructor functions: Animal and Rabbit (classes emulation). How can I set inheritance for this classes using prototypes properly? I seen 2 versions: Rabbit.prototype.__proto__ = Animal....

I m dynamically generating 20 input field type of array in html and submitting record and storing in database. For Example for particular one input field is coded as below. similar there are other 20 ...

I m dynamically generating 20 input field type of array in html and submitting record and storing in database. For Example for particular one input field is coded as below. similar there are other 20 ...