Unable to modify setInterval attribute to improve game responsiveness (JavaScript)

I am currently developing a game and I'm looking to adjust the setInterval property to enhance the responsiveness of a spaceship in the game (currently set as setInterval(loop, 1000 / 40)). To achieve this, I have added a range slider with values ranging from 1 to 3. Below is the code snippet:

Range Slider:

<input type="range" id="sensibilty" min="1" max="3" value="2">

Sensitivity Adjustment:

const loop = function() {
if (keys[37] || keys[65]) {pos.left -= 10}
if (keys[39] || keys[68]) {pos.left += 10}
if (keys[38] || keys[80]) {pos.top -= 1}
if (keys[40] || keys[75]) {pos.top += 1}}
let sens = setInterval(loop, 1000 / 40);
let sensitivity = document.getElementById("sensibilty").value;
if (sensitivity == "1") {clearInterval(sens); setInterval(loop, 1000 / 30)}
else if (sensitivity == "3") {clearInterval(sens); setInterval(loop, 1000 / 60)}

The current setup is not functioning properly, can anyone suggest adjustments to the setInterval values? Thank you!

Answer №1

In order to properly manage your interval, it is important to store it for later use.

let interval = setInterval(loop, 1000 / 40) //ENSURE APPROPRIATE TIMING

if (sensibilty == 1) {clearInterval(interval); setInterval(loop, 1000 / 30)} 
else if (sensibilty == 3) {clearInterval(interval); setInterval(loop, 1000 / 60)} 

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

JavaScript: Efficient ways to extract necessary elements from an array

Currently, I am in the process of creating a tokenizer that constructs an abstract tree. My specific goal is to gather all the "text" elements from an array produced by this tokenizer. The output looks like this: { "error": false, "tokens": [ { ...

Strip all null entries from the URL string

I'm attempting to eliminate all empty parameters from a URL string. This is how my URL appears: http://localhost/wm/frontend/www/?test=&lol=1&boo=2 The desired output from my code should be: http://localhost/wm/frontend/www/?lol=1&boo=2 ...

JQuery could not retrieve the property 'json' from an undefined or null reference

Currently, I am in the process of developing a Windows 8 app using HTML and accessing data from an API. Unfortunately, I keep encountering an error: 0x800a138f - JavaScript runtime error: Unable to get property 'json' of undefined or null refer ...

Tips for utilizing a function on an object

I was given a task that involves working with two arrays filled with random strings in Test.data. The goal is to create a single list containing elements alternating between the two arrays. For example: a: ['a', 'b', 'c'] ...

Add a span element right after the button

Searching for a way to add a span tag following each button element. Here's my current code: <button>button text 1</button> <button>button text 2</button> <button>button text 3</button> And here' ...

What is the proper way to pass arguments to a function within another function in JavaScript?

I am currently working on creating a simple app using jQuery, but I've hit a roadblock. I have two basic functions - the first one holds an event and an id, and the second function is where I want to call the first function. Below is my code: <!DO ...

Discover information about the client using the node.js net library

I have implemented a nodejs cluster with socket.io, where the master thread uses the net library to listen on a port and pass connections to workers based on the client's IP address. Everything is functioning properly, but I am interested in testing b ...

Tips for efficiently importing a file or folder that is valuable but not currently in use

I'm struggling to find any information about this particular case online. Perhaps someone here might have some insight. I keep getting a warning message saying 'FournisseursDb' is defined but never used no-unused-vars when I try to import t ...

Uploading and saving data to an object in FaunaDB using React hook forms

I am currently facing an issue with uploading/saving data to an object in FaunaDB. Specifically, I am trying to upload a string to a jobProfile object: data: { "jobProfile": { "image": "" "coverImage": " ...

What is the best way to set the length of an undefined item in an object to 0 in reactjs?

I am facing a challenge keeping track of scores within each article and displaying them accurately. The issue arises when there is a missing "up" or "down" item in the object. Below is the data containing all the votes: const votes = { "1": { "up": ...

Surprising outcome encountered while trying to insert an item into a list

I'm a bit puzzled by the current situation where: let groupdetails = { groupName: "", }; const groupsArray = []; groupdetails.groupName = 'A' groupsArray.push(groupdetails) groupdetails.groupName = 'B' groupsAr ...

It is not possible to access attributes of an object within the body as it will return as "undefined"

I have encountered a peculiar issue in my Node.js application. When I perform a post request, one of the variables passed in the form is an object called "place": {"id":"0ba78ba1-c7dc-440f-b166-4b2cd6dceff0","coordinates":{&qu ...

HTML Application for Mac and Windows

I am interested in developing a program using HTML/CSS/JS, and I have explored options like cordova and phonegap. However, my goal is to create a Mac/Windows app, not an iOS or Android app. From what I understand, cordova and phonegap do not offer the capa ...

Executing a function that includes showModalDialog outside of an iframe might display the incorrect page

This website structure looks like: `--main.html `--dialog.html `--folder `--iframe.html The code for each page is as follows: main.html: <html> <head> <script type="text/javascript"> function t ...

Storing count variable upon closing Dialog in a React application

I have a situation where I am utilizing useState within a Dialog component in React Material UI. In addition, I am using the useCallBack function in the following manner. const [count,setCount] = React.useState({ count: 0 }) const countCallbac ...

Every time $injector.get is invoked, it triggers the run() method of the Angular module

I find myself in a situation where I need to manually retrieve objects from the Angular $injector. Up until now, I have been using the following approach: var injector = angular.injector(['app.service', 'ng']); var myService = injecto ...

The controller is unable to access the emitted data

I am having an issue with sending data from a service to a controller using $emit. When I log the data in the service, it is present, but when I try to access it in the controller using $rootScope.$on, the data is not showing up. service.GetTest = funct ...

Error code 70006 encountered in Typescript when attempting to reference a type as "any"

Currently, I am working on a React project that involves using TypeScript. This is quite new to me, and I have encountered a type error in one of my components... let dragStart = (e) => { let transferringData = e.dataTransfer.setData("text", e.tar ...

Flow for requesting passport breaks in federated accounts

While using a basic Google Tutorial app, I came across an issue where if a user successfully authenticates with Google and then my app cancels the login process (for example, because the user was not found in the database), the Passport flow breaks. The me ...

Combining various variables into a single input field

I'm attempting to insert multiple JS variable values into a single textbox in HTML, however, only the initial variable is appearing. Is there a method to exhibit all variables (two floats, two strings) in one textbox, or should I explore an alternati ...