The value of a variable remains constant even when it is assigned within a function

I developed a dynamic image slideshow using HTML and JS specifically for the Lively Wallpaper app.

This app offers various customization options, which are stored in the LivelyProperties.json file along with input types such as sliders and text boxes.

To access these properties, one needs to define a function called

function livelyPropertyListener(name, val){}
within the HTML code.

Inside the .json file, there is a slider that can be adjusted through the app's menu. When the slider value changes, it updates the number displayed in a <div> using .innerHTML, however, the variable transitionDuration retains its old value.

The animation function anime used in the code is from the anime.js library.

Upon opening the .html file in Firefox, no errors are reported, indicating that the internal JavaScript functions properly.

        var textEl = document.querySelector(".text");
        var transitionDuration = 3000;
        var imageInterval = 5000;

        function livelyPropertyListener(name, val) //I assume this function is recognized when the .html file is parsed in the app
        {
            if(name =="transitionDuration")
            {
              transitionDuration = val; //transitionDuration stays 3000 (the value in the beginning of the code)
              textEl.innerHTML = val; //this works and the text updates as the slider is moved (due to app limitations file needs to be reloaded for it to updated, but that's fine)
            }
        }

        var ScaleEasingFunc = 'easeInQuad';

        anime({
          targets: '.transition',
          opacity:0,
          direction: 'alternate',
          loop: true,
          easing: 'easeInOutCubic',
          delay: imageInterval/2,
          endDelay: imageInterval/2,
          duration: transitionDuration,
          loopBegin: function(anim) {
            changeImage();
          },
        });

        anime({
            targets: '.scale',
            scale:1.1,
            translateX: screen.width*-1/100,
            translateY: screen.width*1/100,
            direction: 'alternate',
            loop: true,
            easing: ScaleEasingFunc,
            duration: imageInterval + 2*transitionDuration,
        });
</script>

Answer №1

Consider the sequence of events:

  1. var transitionDuration = 3000; sets a value for transitionDuration
  2. function livelyPropertyListener creates a new function
  3. anime({...}) retrieves the current value of transitionDuration, assigns it to an object property, and then passes the object to the anime function
  4. When you invoke livelyPropertyListener, it updates the value of transitionDuration

It's important to note that step 4 does not alter the past to reflect that transitionDuration had already been updated when it was read in step 3.

If you wish to modify the behavior of the anime function after initialization, it should return a value that offers an API allowing you to adjust the duration.

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

What is the best method to determine the currency associated with the code in dinero.js?

Is there an easy way to locate the dinero currency in the dinero.js/currencies package using its code? For example, a function called getCurrency that accepts a string as input and outputs the corresponding dinero currency. ...

Creating unique shaders with THREE.ShaderLibLearn how to craft personalized shaders using THREE

I've been diving into the world of THREEJS shader materials. So far, I've grasped the concept of how uniforms, vertexShader, and fragmentShader work together to manipulate and color vertices and fragments in the realm of glsl and webgl. I've ...

What is the best way to convert a Kotlin Flow into non-streaming JSON data with the Content-Type set to application/json in Spring Framework 6?

Method interface for controlling: @RequestMapping( method = [RequestMethod.GET], value = ["/list"], produces = ["application/json"] ) suspend fun list(): ResponseEntity<Flow<MyClass>> Implementation ...

Tips for eliminating all line breaks in a Node JS application's console log statement

I am currently working on a NodeJS application using Express. While logging is functioning correctly for most files and libraries, I have noticed that many of them, even those beyond my control, contain line breaks in the logs. My objective is to ensure ...

Generate a Report in Embedded Mode using the PowerBI API

Attempting to generate a new report using data from an embedded view, but encountering continuous "This content isn't available" messages and receiving a 403 error from the reportEmbed.min.js during rendering. While able to create and save reports suc ...

How can I use JQuery to sum up the numbers within div elements?

Is there a way to automatically number the generated blocks? For example, the first block would display "1", the second "2" and so on. Below is my current code. Simply input the desired number of blocks in the input field. <!DOCTYPE html> <html ...

Creating an input range element and its event handler dynamically within the ajaxStop function allows for real-time updates based on the

As a beginner in JavaScript development and Ajax, I am currently working on creating a webpage that utilizes Ajax to fetch data from a server. The data is then used to draw geoJSON features on a map using Leaflet. These feature sets need to be toggleable f ...

Methods for organizing consecutive elements within an array in Javascript/Typescript

Let's explore this collection of objects: [ { key1: "AAA", key2: "BBB" }, { key1: "BBB", key2: "CCC" }, { key1: "CCC", key2: "DD ...

After deploying a Next.js project on an Ubuntu server, dynamic pages are returning a 404 error

I've been putting in a substantial amount of time on this project. I'm encountering some 404 errors on my dynamic pages, even though they work perfectly fine on localhost. I've tried using revalidate and playing around with fallback: true an ...

How to activate a button only if the specified conditions are met using VueJS 3

I'm currently facing challenges while working on a form to enable a button when certain conditions are met. The form I created includes fields for name, phone number, options, and a message. Once all requirements are filled, I aim to re-enable the di ...

Encountering module error 'internal/fs' after updating to Node 7

We have encountered some challenges after attempting to update our build server to node v7.0.0. Specifically, the application build task fails at the "bower_concat" step with the following error message: Loading "bower-concat.js" tasks...ERROR Error: Cann ...

Struggling with JSON parsing on iPhone? json-framework can help!

Currently, I am utilizing the json-framework and require assistance in parsing some JSON data received from my server. The structure of the JSON data is as follows: I have already installed the json-framework but I am struggling to parse it correctly. Ca ...

The robot will automatically update its own message after a designated period of time

I am facing an issue with my code where the bot is not updating its message after a specific time period defined by time.milliseconds * 1000. How can I make the bot edit its message after that duration? let timeout = 15000; if (author !== null && ...

Tips for resolving jQuery conflict problems

I am dealing with a jQuery issue where I have two scripts - one for the slider and the other for a dropdown menu. When I remove one script, the slider works but the dropdown doesn't, and vice versa. I have looked at tutorials online on how to resolve ...

Is a missing dependency causing an issue with the React Hook useEffect?

I've encountered an issue with the following code snippet, which seems to only depend on [page]. Despite this, I am receiving the error message: React Hook useEffect has a missing dependency I've come across similar discussions suggesting to com ...

Having trouble importing d3.js and typescript in IntelliJ?

Encountering errors in browsers' console when utilizing d3.select() in typescript code. Despite trying alternative methods like d3-timer.now(), the issue persists. As a newcomer to typescript, I am utilizing intelliJ Ultimate 2019.1. Through npm, I h ...

Combining HTML, CSS, JAVASCRIPT, and PHP for a seamless web development experience

As a beginner in web development, I have some knowledge of javascript, html, css and am currently delving into php. However, I am struggling to find comprehensive resources that show how to integrate all these languages together. I would greatly appreciat ...

Using Telerik Grid in MVC to access the object that is passed to ClientEvent functions on the Javascript side

Here's a challenging question I have. I'm working with a Telerik Grid that has some ClientSide Events: .ClientEvents(events => events .OnDataBinding("SetAjaxParameter") ) Within the SetAjaxParameter function, I am setting parameters for ...

How to extract and process multiple values of the same key in a JSON file using

I'm new to working with JSON and I'm a bit confused about something. What I am attempting to do is to parse the following data: [{"name":"Djinnibone"},{"name":"Djinnibutt","changedToAt":1413217187000},{"name":"Djinnibone","changedToAt":141321720 ...

Error encountered with Three.js SceneExporter due to Uncaught Syntax issue

Recently, I've been attempting to export a three.js scene using the SceneExporter tool. Here is the code snippet I have been working with: var output = new THREE.SceneExporter().parse(scope.renderingEngine.scene); Unfortunately, this has resulted in ...