Bug Tracker

Opened 9 years ago

Closed 9 years ago

#7794 closed bug (invalid)

Bug with event handling?

Reported by: melchior.corgie@… Owned by: melchior.corgie@…
Priority: undecided Milestone: 1.6
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:

Description

Hi,

I join you the following example of code. To see what is the problem, just click a few times on the buttons. The browser will very soon be slowing for the "bugged" button, until it does not respond anymore. Tested with Google Chrome and IE9 beta.

<!doctype html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"> </script>
	<script type="text/javascript">
		function SslFuncBugged() {
			$("a.sslgoogle-bugged").attr("href", "https://www.google.com");
			$("a.sslgoogle-bugged").text("Google.com over SSL");
			
			$("#ssl-button-bugged").text("Ungo SSL... (bugged)");
			$("#ssl-button-bugged").click(NoSslFuncBugged);
		};
		
		function NoSslFuncBugged() {
			$("a.sslgoogle-bugged").attr("href", "http://www.google.com");
			$("a.sslgoogle-bugged").text("Google.com");

			$("#ssl-button-bugged").text("Go SSL! (bugged)");
			$("#ssl-button-bugged").click(SslFuncBugged);
		};
		
		function SslFunc() {
			$("a.sslgoogle").attr("href", "https://www.google.com");
			$("a.sslgoogle").text("Google.com (SSL)");
			
			document.getElementById("ssl-button").innerHTML = "UnGo SSL...";
			document.getElementById("ssl-button").onclick = NoSslFunc;
		};
		
		function NoSslFunc() {
			$("a.sslgoogle").attr("href", "http://www.google.com");
			$("a.sslgoogle").text("Google.com");

			document.getElementById("ssl-button").innerHTML = "Go SSL!";
			document.getElementById("ssl-button").onclick = SslFunc;
		};

		
		$(document).ready(function() {
			NoSslFunc();
			NoSslFuncBugged();
		});
	</script>
</head>
<body>
	<div>
		<a class="sslgoogle" href="http://www.google.com">Google.com</a>
		<button id="ssl-button">Go SSL!</button>
	</div>
	<div>
		<a class="sslgoogle-bugged" href="http://www.google.com">Google.com</a>
		<button id="ssl-button-bugged">Go SSL! (bugged)</button>
	</div>
</body>

What seems to cause problems is the assignation of the event handler for the click event of the button.

Change History (3)

comment:1 Changed 9 years ago by ajpiano

Owner: set to melchior.corgie@…
Status: newpending

It is not clear at all what the actual "problem" is here, aside from the fact that if you keep on adding lots of event handlers on every event, the speed of execution will gradually become slower and slower. That is not a jQuery problem, but a problem for your application.

I cannot fathom a usecase in which it would be necessary to have two handlers, the first of which rebinds the second on every click, and the second of which rebinds the first on every click.

If you can provide any evidence of an actual, bona-fide jQuery bug, then we'll be happy to investigate this issue further. If your concern is an amorphous, "my code slows down when i add a boatload of event handlers recursively," then there is little we can do to assist.

comment:2 Changed 9 years ago by melchior.corgie@…

Status: pendingnew

Ok, my mistake.

Seems like I missed my chance to read the f... manual, so I could have seen that since v1.4.2 jQuery enables me to add several events to one object, and that .unbind() was the solution to my "problem". My misunderstanding was due to my thinking .bind() would reassign the handler every time I would call it, and not enqueue them. This is what I wanted to show with the example of code I gave, which was not a real application.

Sorry for that, you can change the status of this ticket to "newbie waste of time".

comment:3 Changed 9 years ago by ajpiano

Resolution: invalid
Status: newclosed

Heh, since that's not an available status, I'm gonna go with 'invalid' instead :) Thanks for your time and interest in helping jQuery!

Note: See TracTickets for help on using tickets.