Stop the JS long polling reconnect loop if in the incorrect state

#2125