Having trouble making setTimeout work with a nested function in it

I've encountered an issue with the setTimeout() function. Take a look at my code snippet below:

$('#start').click(function(event) {
    var result = Math.floor(Math.random() * (10000 - 4000 + 1)) + 4000; // generates a random value between 4000 and 10000 and stores it in a variable 
    setTimeout("$('.reflex').css('background','red')",result); // this part works fine
    setTimeout(startTimer(),result); //this line runs instantly without delay
});

Do you see the problem? Using a function inside setTimeout seems to bypass the intended time delay and executes immediately.

Any suggestions on how to resolve this?

Answer №1

setTimeout requires a function to be passed in as its first parameter, not the result of the function.

setTimeout(runFunction, delayTime);

Remember, do not include parentheses after the function name runFunction.

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

Is there a way to merge two functions that are triggered by the same class name?

Whenever I click on an item inside my jQuery datatable, a function is triggered. $('.table tbody').on( 'click', '.item', function (e) { alert("clicked"); }); There is also another item with the same class outside the data ...

If the template variable is empty, the nesting HTML element should be hidden

Check out my awesome template: <li><a href="/{{ user_data.room2 }}" id="room2">/{{ user_data.room2 }}</a></li> <li><a href="/{{ user_data.room3 }}" id="room3">/{{ user_data.room3 }}</a></li> < ...

An alternative method to confirm the checkbox selection without physically clicking on it

Currently, I'm in the process of creating a basic to-do list and have been attempting to connect the task with its corresponding checkbox. My goal is for the checkbox to automatically be checked when the task is clicked. Instead of using HTML to add ...

What is the best way to utilize the .done() callback in order to execute a function when new data is loaded upon request?

I have a web page that pulls data from a JSON feed, and there's also a button to load more content from the feed when clicked. I want to add additional elements inside the page for each item in the feed. I've managed to create a function that doe ...

Using React Native to showcase a background grid of dimensions {height: 10, width: 10}

Hey there! I'm looking to create a grid that can adapt to different device sizes and screen positions, similar to a chess grid. I want to use this as a background to help with sizing and positioning elements like text and images on the screen. Here&a ...

Updating user credits through the Model::factory() method is a common practice in my workflow

I need to update the UserCredits after a purchase. For example, if a user currently has 10 credits and purchases 10 more, I want to add the purchased credits to their existing total. The goal is to increment the current credit score with the new credits ...

Is there a way to display the JavaScript widget exclusively on the homepage or main page

How can I limit the display of a Twitter widget on my blog page to only show on the main page and hide it when visitors navigate to sub-pages or individual blog entries? ...

Encountering a roadblock while trying to work with AngularJS Material radio buttons

In one of my projects, I have implemented a polling system where users can choose a question from a list and then proceed to the options page. On the options page, users can select their answer choices and submit their responses. The results are then displ ...

Switching the ASP.NET XHTML Display Mode

I have been given HTML code from a designer to create a master page. The doctype in the HTML is set to HTML 4.0 Strict. The meta tags in the HTML do not contain closing end tags (they end with > instead of />) and the HTML is compliant when checked ...

Steps to deactivate a JavaScript function once the page has undergone a Page.IsPostBack event

My current setup involves a simple div with the display set to none. Upon page load, I use $("#MyDiv").show(); to display the div after a delay, allowing users to enter information into the form and submit it using an asp.net button. After submitting the ...

What is the method to trigger a function upon opening an anchor tag?

When a user opens a link in my React app, I need to send a post request with a payload to my server. My current strategy involves using the onClick and onAuxClick callbacks to handle the link click event. However, I have to filter out right-clicks because ...

Utilizing ASP.NET MVC with AjaxFileUpload

Hey there, I've been struggling with this issue for quite some time now. I really need to perform an asynchronous upload using ajaxFileUpload. While I can successfully post the file to my controller, when trying to return a JsonResult, I keep encounte ...

Simply click and drag a document from your file explorer directly into the desired text field to instantly generate a clickable

I am interested in dragging a file from our Windows server and dropping it onto a text area on a webpage. The link that would be generated will look something like this: <a href="\\fileserver\folder\pizza_2.pdf">filename.pdf< ...

Presentation: Positioning Next Buttons Beside Slide Images While Maintaining Responsiveness

I am facing a challenge with my slideshow project. The slideshow consists of images and text on each slide, along with previous and next buttons that are aligned within the parent container. I am trying to align these buttons at the midpoint of each image ...

Steps for creating a toggle switch that triggers a unique animation on an item

Is it possible to create a left bar that, when clicked, animates to CSS left 10, and then reverts back to its default CSS position when clicked again? I attempted to achieve this using the following code: $('.left-bar').click(function() { $ ...

Troubleshooting: React CSS Transition Fails to Trigger on Exit and Entry

This code is located inside my component file <CSSTransition classNames="method" in={radio === 'add-card'} exit={radio !== 'add-card'} timeout={500} unmountOnExit> < ...

The Datejs library is experiencing issues following an upgrade to jQuery 3.x

I'm currently working on a node.js project that utilizes the Datejs library. However, after updating our local jQuery file from version 1.9.1 to 3.6.0, this library has stopped functioning properly. Here is a snippet of the code: var today = Date ...

Troubleshooting Chrome's autoplay video problem with Fancybox

I've been encountering an issue with Fancybox acting as a lightbox. My videos, which are set up in external HTML documents and loaded into Fancybox when the link is clicked from the main document, do not autoplay in Chrome but seem to be working fine ...

Why isn't the document.getElementById().value() function functioning properly within my PHP code?

When working on my PHP code, I included the following: <select class="ht__select" onchange="sorting('<?php echo $id ?>')" id="sorting"> <option value="">Default sorting</option> <option value="low_price">Sor ...

Form Input Field with Real-Time JavaScript Validation

Is there a way to validate the content of a textarea using pure JavaScript, without using jQuery? I need assistance with this issue. <script> function validate() { // 1. Only allow alphanumeric characters, dash(-), comma(,) and no spaces ...