I just threw this little mouse tracking example together when I realized, that it throws thousands of errors in the chrome console.
Uncaught TypeError: Cannot read property 'clientX' of undefined
AFAIK this means, that chrome can’t find the object that this property belongs to. But if I open the example, it shows the right coordinates perfectly fine. Please help me to get rid of these useless errors.
<html>
<head>
<style type="text/css">
#tracking {
width: 300px;
height: 300px;
background-size: 300px 300px;
background-color: #00F;
left: 100px;
top: 100px;
position: absolute;
}
</style>
<script type="text/javascript">
var mX, mY;
var track;
document.onmousemove = update;
function trackload()
{
track = document.getElementById("tracking");
setTimeout("update()",10);
}
function update(e)
{
mX = e.clientX;
mY = e.clientY;
if (track) {
track.innerHTML = "X: " + mX + " Y: " + mY + " deg: " + (Math.atan(mY / mX) * (180/Math.PI));
track.style.cssText = "-webkit-transform: rotate(" + (Math.atan(mY / mX) * (180/Math.PI)) + "deg);";}
setTimeout("update()",10);
}
</script>
</head>
<body>
<div id="tracking" onclick="trackload()"></div>
</body>
Remove the calls to
setTimeoutto prevent the error. It occurs because theupdatefunction expects an event object, which is not passed in when you call the function through thesetTimeout. Thereforeeis undefined, and you get your error.Here’s a working example.
If there was a good reason for the
setTimeoutcalls, you will need to explain it as I can’t work out why they are needed at first glance. As a side note, passing a string tosetTimeoutis generally considered bad practice (it’s an alias foreval). You can just pass a reference to the function.