JQuery Beginners Tutorial: One Time Events

Say you want to execute an event only once, like the click event. One way to do it would be to set up a counter variable and check if it has reached one. The other way is to use the one event.

In the following example the function executed by the oneTimer button will only run once because it has the one time event.

<html>
<body>
<button id="oneTimer">Click Me</button>
<button id="manyTimes">No Click Me</button>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
	$("button#oneTimer").one("click",function(){
		alert("you won't see this alert again, am using .one()");
	});
	
	$("button#manyTimes").click(function(){
		alert("click me again to see this alert");
	});
});
</script>
</body>
</html>
blog comments powered by Disqus