Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

My collaborative stars - not prettified:

51 views
Skip to first unread message

Gretchen Hall

unread,
Jan 28, 2020, 7:35:36 PM1/28/20
to Intro to JavaScript
var table = document.getElementById('star_table');
var stars = table.getElementsByTagName('img');

var updateStars = function () {
      if (this.readyState == 4 && this.status == 200) {
        for (var num in this.responseText) {
          setStar(num, this.responseText[num] == "1");
        }          
    }
}

var setStar = function(starnum, state) {
   if (state) {
     stars[starnum].src = "star_on.gif";
   } else {
     stars[starnum].src = "star_off.gif";
   }
}
var clickStar = function(evt) {
  var target = stars[evt.target.id];
  if (target == null) {
    return;
  }
  var targetnum = target.id.split("_")[1];
  
  var state = target.src.endsWith("star_on.gif");
  var req = new XMLHttpRequest();
  var params = `n=${targetnum}&s=${state?"0":"1"}`;
  req.open('POST', '/collab.py');
 //Send the proper header information along with the request
  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  req.onreadystatechange = updateStars;
  req.send(params)
}

var requestUpdate = function() {
    var req = new XMLHttpRequest();
  req.open('GET', '/collab.py');
  req.onreadystatechange = updateStars;
  req.send()
}

table.addEventListener("click", clickStar, false)

window.setInterval(requestUpdate, 1000);

Reply all
Reply to author
Forward
0 new messages