From the Super Mario Wiki
Upon using the Thunder Stick, the racer will begin to repeatedly swing it for approximately 5 seconds. If a rival gets too close to the attacking racer, the rival will get zapped, spin out, and be brought to a complete stop.
A variant of the Thunder Stick known as the Thunder Stick+ exists exclusively in Mario Kart Arcade GP. This variant extends the duration of the attack by around 2 seconds.