Created
March 4, 2016 20:02
-
-
Save beckybeauchamp1/9a993f52ce4ba621edc6 to your computer and use it in GitHub Desktop.
Starting and Stopping Click Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Example</title> | |
</head> | |
<body> | |
<button id="start">Start Game</button> | |
<button id="stop">Stop Game</button> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
// click events for start and stop | |
function addStartEventListener(){ | |
$("#start").on("click", startGame); | |
} | |
function addStopEventListener(){ | |
$("#stop").on("click", resetGame); | |
} | |
// call back functions | |
function startGame(){ | |
event.preventDefault(); | |
$("body").append("<div>START CLICK EVENT</div>"); | |
$("#start").off("click"); | |
// calling function to intiate event listener for stopping the game | |
addStopEventListener(); | |
} | |
function resetGame(){ | |
event.preventDefault(); | |
$("body").append("<div>STOP CLICK EVENT</div>"); | |
$("#stop").off("click"); | |
// calling function to re-intiate event listener for starting | |
addStartEventListener(); | |
} | |
addStartEventListener(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment