Priority switching in JS