Alert from inside function



  • I want to display a notification every time the interval runs. I get this error message:

    Uncaught TypeError: this.sendNotification is not a function at OV.js:40

    I think is thas something to do with where I place it because if I place it outside the interval function it does work. Maybe someone can point me in the right direction.

    Module.register("OV",{
    // Default module config.
    defaults: {
    	text: "Openbaar Vervoer!lsdksdkk"
    },
    
    // Override dom generator.
    getDom: function() {
    	var wrapper = document.createElement("div");
    	
    	function updateOV() {
    	this.sendUpdate;
    	var xhttp = new XMLHttpRequest();
    	xhttp.onreadystatechange = function() {
    		if (this.readyState == 4 && this.status == 200) {
    		var string = "";
    		//console.log(this.responseText);
    		var data = JSON.parse(this.responseText);
    		//console.log(decodeURI(data.dest));
    		string += "<table>";
    		$.each(data, function( index, value ) {
    			string += "<tr>"
    			console.log(value );
    			string += "<td>" + value.service + "</td>";
    			string += "<td>" + decodeURI(value.dest) + "</td>";
    			string += "<td>" + value.time + "</td>";
    			string += "<td>" + value.state + "</td>";
    			string += "</tr>";
    		});
    		string += "</table>";
    		wrapper.innerHTML = string;
    		}
    	  };
    	  xhttp.open("GET", "http://192.168.1.167/esp/bustijden.php");
    	  xhttp.send();
    	}
    	
    	setInterval(function() {
    		updateOV();
    		this.sendNotification("SHOW_ALERT", {title: "OV", message: "Bustijden ge-update.", timer: "1000"});
    	}, 10000);
    	updateOV();
    	
    	return wrapper;
    },
    
    getScripts: function() {
    return [
    	'https://code.jquery.com/jquery-2.2.3.min.js',  // this file will be loaded from the jquery servers.
    ]
    },
    
    });
    


  • @Luukth thats an issue of accessing the right scope, you can fix that by using an arrow function

    setInterval(() => {
      updateOV();
      this.sendNotification("SHOW_ALERT", {title: "OV", message: "Bustijden ge-update.", timer: "1000"});
    }, 10000);
    


  • @strawberry-3.141 Thanks that did the trick.



  • @Luukth I suggest to outsource the data query from the render function



  • @strawberry-3.141 said in Alert from inside function:

    I suggest to outsource the data query from the render function

    Can you maybe link me to a documentation. I can’t really find a good way to learn how to make al of those functions.




Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.