I played around the MAME debugger to see what I could learn about how the worms behave when stunned. This isn't code-level analysis so I can't claim that it's exhaustive or perfect, but all my observations seem to match the behavior described below.
Basically, each worm has a stun timer, and the timer counts down one value every two frames. 0 is included the time.
The upper vine worms on the gold board have the simplest stun behavior. Each spray sets the stun timer to $80, or 128. Since each value gets two frames, including 0, this means the upper vine worms will remain stunned for 258 frames. Every subsequent spray resets the timer to $80, no matter how fast or often they are sprayed, or if they've already been stunned and then un-stunned.
Ground worms are a bit more complicated. As most players know, the more you spray them the longer they remain stunned. There is, of course, a limit to this.
Each spray of a ground worm adds $30 to the timer, and the timer maxes out at $FF. So, with frame-perfect spraying directly underneath a worm it would only take 6 sprays to max out the timer ($30 > $60 > $90 > $C0 > $F0 >$FF). It would actually only take 6 frame-perfect frames to stun them from any position, but the window is a little tighter the further away you are.
The ground worm timer decrements the same way as the vine worms: one value every two frames, with 0 counting for two frames. So each $30 of the timer equates to 98 frames and the maximum timer value of $FF equates to 512 frames.
The crucial part of the ground worm stun cycle is that each spray only adds $30 to the timer, up to $FF; it doesn't matter how long the worm has been stunned or if it has been previously stunned. The longer you wait between sprays the more sprays it will take to reach $FF. In other words, if you get the maximum stun in the first 6 sprays, then wait 511 frames to spray again, it will take another 6 sprays to reach the full stun value again. So, if you delay successive sprays while dealing with other stuff on the board, it will take more and more sprays to reach the full stun value. If you spray twice, then deal with other bugs for too long, it might take another full 6 sprays to reach $FF instead of 4.
Vines Worms (Gold Board)
| Frames | | | | | Real Time (60 Hz) | | | | | Real Time (60.6061 Hz) |
Added to Timer (each spray) | 258 | | | | 4.300 seconds | | | | 4.257 seconds |
Maximum Timer | 258 | | | | 4.300 seconds | | | | 4.257 seconds |
Ground Worms (All Boards)
| Frames | | | | | Real Time (60 Hz) | | | | | Real Time (60.6061 Hz) |
Added to Timer (each spray) | 98 | | | | 1.633 seconds | | | | 1.617 seconds |
Maximum Timer | 512 | | | | 8.533 seconds | | | | 8.448 seconds |