JavaScript Can I make a portion of JS run continously?

I'm working on an electron app(first time using electron). I'm also very new to javascript so please excuse my stupidity.

In the index.js file I have this method to check if a network connection exists and update a div element accordingly-

function checkStatus() {
  var online = navigator.onLine;
  if(online){
    $(".net-status").html("<i class=\"fa fa-globe fa-2x\"></i> Connected");
  }
  else{
    $(".net-status").html("<i class=\"fa fa-globe fa-2x\"></i> Not Connected");
  }
}

I want this to run continuously and as soon as the user disconnects from the internet, change the text to "Not Connected"

I tried using a onkeypress event listener in my index.html file but couldn't get it to work.

Also I'm wondering if there is a better way to achieve this purpose than an event listener.

Answer:1

There are event listeners available that fire when there is a change to that variable

window.addEventListener("offline", function(e) {
    $(".net-status").html("<i class=\"fa fa-globe fa-2x\"></i> Not Connected");
});

window.addEventListener("online", function(e) {
    $(".net-status").html("<i class=\"fa fa-globe fa-2x\"></i> Connected");
});
Answer:2

I have created a custom matcher in jasmine to verify if an element has a class or not. This is the code that I have placed in the beforeEach() function to define the custom matcher: beforeEach(...

I have created a custom matcher in jasmine to verify if an element has a class or not. This is the code that I have placed in the beforeEach() function to define the custom matcher: beforeEach(...

I'm trying to make an advanced <select> tag but implementing an input before the first <option> attribute for searching. Something like this: Here is my code. How can I add <input name=...

I'm trying to make an advanced <select> tag but implementing an input before the first <option> attribute for searching. Something like this: Here is my code. How can I add <input name=...

I'm using angularjs for my application and found this issue on google chrome, the problem is I have this URL: http://127.0.0.1/#/fa/episode/1315019/برنامه-خوب but when I click on this links it shows:...

I'm using angularjs for my application and found this issue on google chrome, the problem is I have this URL: http://127.0.0.1/#/fa/episode/1315019/برنامه-خوب but when I click on this links it shows:...

I am using React/Redux for my web-app and I have the Description Class where user can edit description. Props description and propertyTypes are coming from AJAX calls. import React, { PropTypes } ...

I am using React/Redux for my web-app and I have the Description Class where user can edit description. Props description and propertyTypes are coming from AJAX calls. import React, { PropTypes } ...

  1. update nested value javascript
  2. mongodb update nested value
  3. jq update nested value
  4. redux update nested value
  5. update nested value
  6. python json update nested value
  7. react state update nested value
  8. python dictionary update nested value
  9. update value in nested list c#
  10. update value of nested dictionary
  11. update value in nested dict python