The box remains static as the mouse moves rapidly – JavaScript exclusive

function down(event){
    event.target.style.backgroundColor="green";
    document.addEventListener("mousemove",move,false);
}

function up(event) {
    event.target.style.backgroundColor="red";
    document.removeEventListener("mousemove",move,false);
}

function move(event) {
    event.target.style.left=Math.max(0,Math.min(window.innerWidth-50,event.clientX-25))+"px";
    event.target.style.top=Math.max(0,Math.min(window.innerHeight-50,event.clientY-25))+"px";
}

For the complete code, you can visit -> http://jsfiddle.net/tcubsfbg/1/
I have noticed that when I move the mouse too quickly, the box does not update its position as fast as my mouse movement. It seems like despite adding mouse move events to the window and even with the document, the issue persists.

Answer №1

Resolved the issue by adjusting how the event listeners were attached.

var box=document.getElementById("hello");
function init(){
    box.addEventListener("mousedown",down,false);
    box.addEventListener("mouseup",up,false);
}
function down(event){
    document.addEventListener("mousemove",move,false);
}
function move(event){
    box.style.left=event.clientX-50+"px";
    box.style.top=event.clientY-50+"px";
}
function up(event){
    event.target.style.backgroundColor="red";
    document.removeEventListener("mousemove",move,false);
}
window.onload=init();

http://jsfiddle.net/tcubsfbg/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change array association with object extension

I am looking to transform the assignment below into one that utilizes object spread: bData.steps[bStepKey].params= cData[cKey] My attempt so far has been unsuccessful. bData= {...bData, steps:{ ...bData.steps[bStepKey], params: cData[cKey]}} ...

Enhancing Tooltips with JQuery Tools in an UpdatePanel

My issue involves using the JQuery Tools Tooltip Plugin () with tooltips that are refreshed in asp.net UpdatePanel controls. The problem arises when after adding tooltips to items, any partial postback from an UpdatePanel causes the tooltips to malfunction ...

The OrhographicCamera is having difficulties capturing the entire scene in its render

Recently, I have been working on rendering a scene using three.js and WebGL in an isomorphic manner. In my research, I came across suggestions to use the OrthographicCamera for this purpose. However, upon implementing it, I noticed some strange outcomes. A ...

Obtaining a variety of data points from an XMLHttpRequest

I recently discovered a technique for sending multiple variables on SO. Here is the code snippet I used: xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","http://127.0.0.1:3000?var1=" + name + "&var2=test", true); xmlhttp.send(); xmlhttp.onreadystat ...

How can I redirect to another page when an item is clicked in AngularJS?

Here is an example of HTML code: <div class="item" data-url="link"></div> <div class="item" data-url="link"></div> <div class="item" data-url="link"></div> In jQuery, I can do the following: $('.item').click ...

Enhancing dashboard functionality: Managing and updating database table rows

Currently, I am in the process of creating a function that allows me to edit table row values from my plugin's dashboard. The table contains fields such as first name, last name, street, and city. My initial thought was to use a thickbox function for ...

Problem with Clerk's authentication() functionality

Currently facing an issue with the Clerk auth() helper (auth() documentation) while working with react and next 13 (app router). When trying to access both userId and user from auth(), const { userId, user } = auth();, it seems that userId contains a val ...

What is the process for selectively adding interceptors to app.module?

After searching through various topics, I have not found a solution that addresses my specific issue. To provide some context, we have an Angular App that operates in two modes - one mode uses one API while the other mode utilizes a different API. My goal ...

The JavaScript jump function quickly moves back to the top of the webpage

Issue Resolved To prevent the view from jumping to the top of the page, I have implemented three options: Set the href attribute to href="#!" Set the href attribute to href="javascript:;" Pass the object to the event-handler and preve ...

Exploring uncharted territory with the Navigator component in React Native

I am experiencing an issue with undefined navigator when using React Native MessageTabs: _onPressItem = (item) => { const { navigate } = this.props.navigation; //console.log(JSON.stringify(item)); navigate('SingleConversation', {id ...

Is it possible to retrieve the throughput or latency data of a WebRTC stream directly from an RTCPeerConnection object?

Does anyone have suggestions on how to calculate the latency or throughput of a receiving stream in WebRTC? I'm familiar with using getStats(), but can't find a straightforward method. Any tips are appreciated! ...

When trying to revert back to the original content after using AJAX to display new HTML data, the .html() function

Here is the JavaScript I am using to handle an ajax request: $(document).ready(function() { // Variable to hold original content var original_content_qty = ''; $('.product-qty-<?php echo $products->fields[' products_id ...

The AngularJS change event is not being activated

I am a beginner with angular js and I have implemented a bootstrap calendar in my application. However, I am facing an issue where the change event is not being triggered when the month changes, no matter where I place it within the code. Here is the snip ...

Encountering NodeJs Error 401(Unauthorized) while implementing passport-jwt in my project

I am currently developing an authentication application using Node.js, MongoDB, and the Passport-JWT middleware. I have successfully implemented the login functionality and I am able to obtain a token. However, when trying to access the user profile after ...

Tips for retrieving values from CheckBox in Asp.net MVC using Jquery

I'm currently facing a dilemma while working on an MVC web application. I have dynamically generated checkboxes from my database, but I am uncertain about how to extract the value of the selected checkbox and store it in the database. Any suggestions? ...

Using OPTIONS instead of GET for fetching Backbone JS model data

Currently, I am attempting to retrieve data from a REST endpoint with the help of a model. Below is the code snippet that I am using: professors: function(id) { professor = new ProfessorModel({ id: id }); professor.fetch({ headers: { ...

Choose a node from descendants based on its attribute

I am working with an interface that switches between displaying different div elements. Each div element has their children arranged differently, and when the switch happens, I need to access a specific child node of the newly displayed div. However, I fin ...

Anticipate feedback from Python script in Node.js

I have developed a website using nodejs and express, but I am facing an issue with integrating a python script for face recognition. The problem lies in the fact that when I invoke this script from nodejs using child-process, it takes around 10 to 20 secon ...

Utilize Material UI's Datagrid or XGrid components to customize the rendering

There is a section from Material UI discussing renderHeader in the DataGrid and Xgrid components. https://material-ui.com/components/data-grid/columns/#render-header The documentation describes how to add additional content to the header, but what if I w ...

Preventing the onClick function from being triggered when the Enter key is pressed

I am struggling with a div that has subscriptions for both onClick and onKeyPress (Enter click). The desired behavior for a mouse click is: first click - open popup, second click - close popup. The desired behavior for an Enter click is to open the popup ...