Aaa! This has happened to me a few times too. (Compassed into a fence, or teleported on cake). Can't even get control of the mouse. It's as if I was falling on the same block over and over, a fraction of a second at a time, with the cursor/view being reset each time.
Try holding down the jump button and choose a direction to jump. (Ahh, soul sand back in those days).
Or, copy "/home" or "/spawn" somewhere, then in-game, quickly go to chat, then Ctrl+V and enter.
Or to relog, hit Esc and click Disconnect as fast as possible. Once you log in, you may have a few splitseconds of extra time, to jump off.