The setTimeout function in VueJS seems to be malfunctioning and not properly setting the

I am attempting to play sounds in sequence using setTimeout. In my array named challenge, I have (for testing purposes) the values [0,1,2,3]. Additionally, I have a function called play(n, true) for starting the sound and play(n, false) for stopping the sound. My goal is to:

  • play(0, true).wait1seconds.play(0,false).wait1second
  • play(1, true).wait1second.play(1,false).wait1second and so on.

This is what I have attempted up to this point:

watch: {
    turn: function(turn) {
        if (this.turn === 0) {
            var self = this;
            var time = 500;
            var timeArray = [[],[]]; // I tried with and without this array
            for (var i = 0; i < this.challenge.length; i ++) {
                timeArray[0][i] = setTimeout(function() {
                    self.play(i, true);
                }, time);
                timeArray[1][i] = setTimeout(function() {
                    self.play(i, false);
                    this.time += 1500; //  I tried with and without this
                }, time + 1000);
            }
        }
    }
}

When I omit the array, all the sounds play simultaneously and occasionally produce the desired sound. However, utilizing the array results in an object error.

Answer №1

What is the benefit of creating an array if you don't intend to reference the timeouts later? Consider spacing out timers manually by 1 second instead.

// Here's a mock code example:
var task = [0, 1, 2, 3];

for (let i = 0; i < 2 * task.length; i++) {
  setTimeout(function() {
    execute(Math.floor(i / 2), i % 2 === 0);
  }, 1000 * i);
}

function execute(taskIndex, performAction) {
  if (performAction) {
    console.log("executing task: " + taskIndex);
  } else {
    console.log("stopping task execution: " + taskIndex);
  }
}

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

Using JavaScript to automate keystrokes programmatically

Is there a way to programmatically close a webpage using the keyboard shortcut control + W? I'm wondering if I can write code that will mimic this specific shortcut (control+W). ...

Conceal one element and reveal a different one upon submitting a form

On a web page, I want to hide the form upon submission (either by clicking or pressing enter) and display the results. However, this functionality does not seem to work when the Go web server is running. When I test the HTML file (without executing the Go ...

I'm baffled on how to find access to ParametricGeometry within Three.js

I've been looking into how to access ParametricGeometry because I keep encountering this message when I attempt to use it: "THREE.ParametricGeometry has been relocated to /examples/jsm/geometries/ParametricGeometry.js" Any ideas on how to do this wou ...

Optimizing JSON data by recursively removing empty values, converting strings to booleans, and trimming whitespace using jq - best practices?

During data preprocessing, I have the task of eliminating all empty values from an input JSON. This includes removing empty arrays [], empty objects {}, and various forms of empty strings like "", " ", and "\t". Additio ...

Is it possible for a Simplemodal popup to appear only once per user session

I'm completely new to javascript and jQuery. Recently, I've started using SimpleModal basic from SimpleModal to show a popup upon visitors landing on my website. Everything seems to be working perfectly, but there's one issue - the popup kee ...

Continue perusing the Angular JS HTTP request

I'm currently delving into the world of web systems, still a bit green when it comes to Angular JS. The feature I'm tackling now requires a POST call using Angular. Implementing the POST call itself isn't too tricky in Angular. However, my ...

changing the default property value within an object

I'm looking to send an ajax request to a PHP file that will retrieve a user's information in JSON format. I then want to take this data and assign it to a property within an object. For example: User = { prop1: '' } I simply need to ...

Troubleshooting Checkbox Problems on Internet Explorer 6

In order to have checkboxes generated dynamically for a pop-up window utilizing AJAX, I am using Javascript. Furthermore, when a button is clicked, a function needs to be executed that checks all the checkboxes before the pop-up appears. The web pages bei ...

Is it possible to execute custom JavaScript code in an R Jupyter notebook?

Within my Jupyter Notebook, I am working with the R programming language and would like to integrate javascript functions into it. I'm aware that there are libraries in javascript that can be called from R, but I haven't been able to find any ex ...

Protect a one-page application using Spring's security features

After successfully developing a single page application using JavaScript, I incorporated a few jQuery commands and Twitter Bootstrap for added functionality. The method used to load my page is demonstrated below: $('#contact').click(function () ...

Adding a fresh element to an object array in TypeScript

How can we add a specific value to an array of objects using TypeScript? I am looking to insert the value 1993 into each "annualRentCurrent" property in the sample object. Any suggestions on how to achieve this in TypeScript or Angular? Thank you! #Data ...

Tips on transforming JSON data into a hierarchical/tree structure with javascript/angularJS

[ {"id":1,"countryname":"India","zoneid":"1","countryid":"1","zonename":"South","stateid":"1","zid":"1","statename":"Karnataka"}, {"id":1,"countryname":"India","zoneid":"1","countryid":"1","zonename":"South","stateid":"2","zid":"1","s ...

Using Jquery and CSS to display or conceal table elements depending on viewport width

I am seeking advice for my issue. Here is the code snippet on JSFiddle. I have a list of models and corresponding values that vary across different categories. I have created a simple table to display this data. However, I need to alter the table structur ...

What is the best way to split an input field into distinct fields for display on the screen?

Take a look at this image: https://i.stack.imgur.com/LoVqe.png I am interested in creating a design similar to the one shown in the image, where a 4 digit one-time password (OTP) is entered by the user. Currently, I have achieved this by using 4 separate ...

BufferGeometry's Vertices

Since version 125, the use of THREE.Geometry has been deprecated. As we update our code base, we are encountering errors that are proving difficult to resolve. Our current task involves creating a sphere and applying a raycaster on it to determine the int ...

When utilizing React Hooks, passing a prop from a parent component to a child component may result in the prop being undefined in the child component

What is the main goal I am aiming to achieve? My objective is to establish an array of objects where the values are directly linked to the parent component. How am I currently attempting to accomplish this task through code? Here is a simplified breakdo ...

Is there a way to run /_next/static/xxx.js using a script tag?

I have customized my next.config file (webpack) to generate a static JavaScript file (.next/static/loader.js). The original loader.js is an Immediately Invoked Function Expression (IIFE): (function stickerLoader(){ alert('Hello'); // ... so ...

How come my fixed navigation bar is appearing above my sticky navigation bar even though I positioned it at the bottom in the code?

My current project involves creating a sticky bar and fixed navbar using Bootstrap 5. I structured my code with one file for the sticky navbar and another file for the fixed navbar. The challenge I faced was having the fixed navbar overlap the sticky navba ...

The function _path2.default.basename does not work when using convertapi within an Angular framework

I'm currently working on integrating the convertapi into my Angular 11 application by referencing the following documentation https://www.npmjs.com/package/convertapi My goal is to convert PDFs into images, However, I encountered an issue when tryi ...

What makes TypeScript unique is that it is completely written in TypeScript, with TypeScript accounting for 100

According to information on the TypeScript repository on GitHub, it consists of 100.0% TypeScript files (.ts) https://i.sstatic.net/Is7XE.png I am curious about how TypeScript is able to compile itself to JavaScript without any external help. Can you exp ...