Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
<meta name="description" content="Secure voting">
  <title>Secure voting</title>
</head>
<body>
</body>
</html>
 
var ref = new Firebase('https://stackoverflow.firebaseio.com/37950590');
ref.on('value', function (s) {console.log(JSON.stringify(s.val(), null, '  ').replace(/"/g, "'"));});
function update(updates) {
  console.log(JSON.stringify(updates,null, '  ').replace(/"/g, "'"));
  ref.update(updates).then(function() {
    console.log('updated');
  }).catch(function(error) {
    console.error(error);
  });
}
function vote(auth) {
  console.log('vote');
  ref.child('voteCount').once('value', function(voteCount) {
    var updates = {};
    updates['votes/'+auth.uid] = true;
    updates.voteCount = voteCount.val() + 1;
    update(updates);
  });  
}
function unvote(auth) {
  console.log('unvote');
  ref.child('voteCount').once('value', function(voteCount) {
    var updates = {};
    updates['votes/'+auth.uid] = null;
    updates.voteCount = voteCount.val() - 1;
    update(updates);
  });  
}
ref.authAnonymously(function(error, auth) {
  vote(ref.getAuth());
  setTimeout(function() {
    vote(ref.getAuth());
  }, 5000);
  setTimeout(function() {
    unvote(ref.getAuth());
  }, 10000);
  setTimeout(function() {
    unvote(ref.getAuth());
  }, 15000);
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
pufpro
0viewers