Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Script running multiple times

Script running multiple times

Scheduled Pinned Locked Moved JavaScript
debuggingjavascriptwpfwcftools
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnalogNerd
    wrote on last edited by
    #1

    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?

    D A 2 Replies Last reply
    0
    • A AnalogNerd

      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?

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • A AnalogNerd

        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?

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups