Script running multiple times
-
I have a jQuery script that is listening for keydown on a textbox, like so:
$(document).ready(function () {
$("#result").on('keypress', function (e) {
...
});
});My problem is that I have written this for a site architected and hosted by a 3rd party. This site allows you to move modules around the page if you wish. When my module is moved (sometimes! not all the the time) it appears as if the page creates more instances of my code. As a result the code in the keydown function fires multiple times. A few things: 1. I put an alert in the document.ready and can see it pop up multiple times, so I know the page it loading more instances of my javasvript. 2. When looking in the debugger in IE or Firefox I can't find more than one instance of the script, but a breakpoint on the one I can find will be caught only once. Whereas the code does run multiple time. Again and alert in the function can be seen multiple times. I've tried unbinding events from my textbox before binding, but it doesn't work. Does anyone have any ideas of how I can prevent my code from being instantiated or triggered multiple times?
-
I have a jQuery script that is listening for keydown on a textbox, like so:
$(document).ready(function () {
$("#result").on('keypress', function (e) {
...
});
});My problem is that I have written this for a site architected and hosted by a 3rd party. This site allows you to move modules around the page if you wish. When my module is moved (sometimes! not all the the time) it appears as if the page creates more instances of my code. As a result the code in the keydown function fires multiple times. A few things: 1. I put an alert in the document.ready and can see it pop up multiple times, so I know the page it loading more instances of my javasvript. 2. When looking in the debugger in IE or Firefox I can't find more than one instance of the script, but a breakpoint on the one I can find will be caught only once. Whereas the code does run multiple time. Again and alert in the function can be seen multiple times. I've tried unbinding events from my textbox before binding, but it doesn't work. Does anyone have any ideas of how I can prevent my code from being instantiated or triggered multiple times?
You could have a look at what other events are firing when the stuff is moved around. You could then use these to suspend the kepress actions and then re-enable them post move.
Dave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
-
I have a jQuery script that is listening for keydown on a textbox, like so:
$(document).ready(function () {
$("#result").on('keypress', function (e) {
...
});
});My problem is that I have written this for a site architected and hosted by a 3rd party. This site allows you to move modules around the page if you wish. When my module is moved (sometimes! not all the the time) it appears as if the page creates more instances of my code. As a result the code in the keydown function fires multiple times. A few things: 1. I put an alert in the document.ready and can see it pop up multiple times, so I know the page it loading more instances of my javasvript. 2. When looking in the debugger in IE or Firefox I can't find more than one instance of the script, but a breakpoint on the one I can find will be caught only once. Whereas the code does run multiple time. Again and alert in the function can be seen multiple times. I've tried unbinding events from my textbox before binding, but it doesn't work. Does anyone have any ideas of how I can prevent my code from being instantiated or triggered multiple times?
I faced a similar situation when I had rendered the same parent script at multiple locations e.g. master / child pages and so on. You may be facing a similar scenario.
Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial