This code is working great but I’d like to eliminate the need to click the “connect” button in order to populate the data to the webpage. When the webpage loads I’d just like it to automatically populate. Make sense? Any chance you could help me? Thank you!
<html>
<head>
<title>
</title>
</head>
<body>
<br>
<span id="current"></span><br>
<span id="TempF"></span><br>
<br>
<button id="connectbutton" onclick="start()">Connect</button><br>
<script type="text/javascript">
function start() {
document.getElementById("connectbutton").innerHTML = "Waiting for data...";
var deviceID = "x";
var accessToken = "y";
var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);
eventSource.addEventListener('open', function(e) {
console.log("Opened!"); },false);
eventSource.addEventListener('error', function(e) {
console.log("Errored!"); },false);
eventSource.addEventListener('current', function(e) {
var parsedData = JSON.parse(e.data);
var tempSpan = document.getElementById("current");
tempSpan.style.fontSize = "28px";
tempSpan.innerHTML = "Appliance Current: " + parsedData.data;
}, false);
eventSource.addEventListener('tempF', function(e) {
var parsedData = JSON.parse(e.data);
var tempSpan = document.getElementById("tempF");
tempSpan.style.fontSize = "28px";
tempSpan.innerHTML = "TemperatureF: " + parsedData.data;
}, false);
}
</script><br>
</body>
</html>