How to avoid PointerLockControls error


I want to create a pause menu, but I have this error appearing when the player spam clicks to lock the mouse when locking and unlocking the mouse quickly.
Uncaught (in promise) DOMException: The user has exited the lock before this request was completed.

I was wondering if there was a way or a trick to avoid it as 1) it doesn’t lock the mouse 2) make a huge framerate drop each time it happens.

You can store the time when the pointer lock was activated and then whenever you need to toggle it again you check if it was toggled/activated within the last 100 ms or so.

let pointerLockActivatedAt = null;

function inactivatePointerLock() {
    const now =
    if (pointerLockActivatedAt != null && now - pointerLockActivatedAt < 100) {
    // do your thing
function activatePointerLock() {
    const now =
    pointerLockActivatedAt = now

Seems to be a about a 1-second time window in Chrome before pointer can be locked again.