Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bobbravo2/29207957dacabf82e836227b27e0b720 to your computer and use it in GitHub Desktop.
Save bobbravo2/29207957dacabf82e836227b27e0b720 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Click Button with Firebase!</title>
<!-- -->
<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
</head>
<body>
<!-- -->
<h1>Ouch! You've clicked me <span id="clickValue"></span> times!</h1>
<!-- -->
<button id="clickButton">Click Me!!!!</button>
<!-- -->
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<!-- ================================================================================== -->
<script>
//
var config = {
apiKey: "AIzaSyAJS4YQWU5DmESeYueG1qH1NGkjv3DncEY",
authDomain: "fir-click-counter-7cdb9.firebaseapp.com",
databaseURL: "https://fir-click-counter-7cdb9.firebaseio.com",
storageBucket: "fir-click-counter-7cdb9.appspot.com",
};
firebase.initializeApp(config);
// VARIABLES
// --------------------------------------------------------------------------------
//
var database = firebase.database();
//
var clickCounter = 0;
// FUNCTIONS + EVENTS
// --------------------------------------------------------------------------------
//
$("#clickButton").on("click", function() {
//
clickCounter++;
//
database.ref().set({
clickCount: clickCounter
});
})
// MAIN PROCESS + INITIAL CODE
// --------------------------------------------------------------------------------
//
database.ref().on("value", function(snapshot) {
//
console.log(snapshot.val());
//
$("#clickValue").html(snapshot.val().clickCount);
//
clickCounter = snapshot.val().clickCount;
//
}, function (errorObject) {
//
console.log("The read failed: " + errorObject.code);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment