Cannot read property ‘clientX’ of undefined while using console.log

Im trying to make a simple game cheat detector and I have encountered this problem while testing it. My issue is that when that I try printing the Coordinates it gives me this error: Uncaught TypeError: Cannot read property ‘clientX’ of undefined

I think my problem is because of looping, and its probably really simple, but here is my code:

var USrPosX
var USrPosY
setInterval(Chk, 1000)
function Chk() {
 USrPosX = event.clientX
 USrPosY = event.clientY
 console.log(USrPosX)
 console.log(USrPosY)
} 

Answer

The reason you get the error, is because you never specified “event”. By looking what you are trying to do, i assume you want to get the mouse coordinates. if you are listening for “mousemove” event on the document, you will be able to access the clientX and clientY properties:

    var USrPosX;
    var USrPosY;

    document.addEventListener('mousemove', onMouseMove);

    function onMouseMove(event){
        USrPosX = event.clientX;
        USrPosY = event.clientY;
    }

    setInterval(Chk, 1000);

    function Chk(event) {
        console.log(USrPosX)
        console.log(USrPosY)
    } 

you can see that i get the “event” as a parameter in the event listener “onMouseMove”. It is send by the browser whenever the “mousemove” event is triggered.