What could be causing the issue with my progress-bar transition not working properly on mobile browsers?

I've encountered a problem with the bootstrap progress-bar in a mobile browser while developing a Blazor project. The issue arises when I use two buttons to control the progress bar - one to start it and the other to stop it. The animations are managed using the transition property. Strangely, everything works fine on my desktop and laptop, but fails on mobile browsers (although it works when emulated through developer tools).

The problem begins after pressing the Stop button for the first time on mobile browsers. When I try to restart the progress bar by pressing Start again, it doesn't reset properly and instead starts increasing slowly despite having 'transition: none' set. Subsequent presses on the stop and start buttons further break the functionality. A screen recording showcasing this bug on mobile is available on swisstransfer file share service: .

You can find the code below and test the site on Azure.

Any help would be greatly appreciated.


    @page "/"
    
    
    @* The progress bar ===================================================================*@
    <div style="padding-bottom: 5px;">
        <div class="progress" style="height: 10px;">
            <div class="progress-bar" style="width: @_progressPercent%; background-color: @_progressColor; transition: @_transition"></div>
        </div>
    </div>
    
    
    <button disabled="@_startButtonIsDisabled" @onclick="StartProgressBar">Start</button>
    <button disabled="@_stopButtonIsDisabled" @onclick="StopProgressBar">Stop</button>
    
    
    
    @code {
    
        private Timer? _timer;
        private int _counter;
        private string _progressColor="grey";
        private string _transition="none";
        private int _progressPercent=100;
        private double _t1 = 4;
        private double _t2 = 8;
        private double _tend = 16;
        private DateTime _timerStartTime;
        private bool _startButtonIsDisabled = false;
        private bool _stopButtonIsDisabled = true;
    
    
        private void TimerCallback(object? o)
        {
            _counter++;
            switch (_counter)
            {
                case 0:
                    _progressPercent = (int)(100 * ( 1 - _t1 / _tend));
                    _transition = $"width {_t1}s linear";
                    InvokeAsync(StateHasChanged); // Update the UI
                    break;
                case 1:
                    _progressPercent = (int)(100 * ( 1 - _t2 / _tend));
                    _progressColor = "orange";
                    _transition = $"width {_t2-_t1}s linear";
                    InvokeAsync(StateHasChanged); // Update the UI
                    _timer.Change(1000*(int)(_t2-_t1), Timeout.Infinite); // Set timer to _t2-_t1 seconds for the next event
                    break;
                case 2:
                    _progressPercent = 0;
                    _progressColor = "red";
                    _transition = $"width {_tend-_t2}s linear";
                    InvokeAsync(StateHasChanged); // Update the UI
                    _timer.Change(1000*(int)(_tend-_t2), Timeout.Infinite); // Set timer to _tend-_t2 seconds for the next event
                    break;
                case 3:
                    InvokeAsync(StateHasChanged); // Update the UI
                    _timer.Dispose(); // Stop the timer
                    break;
            }
        }
    
    
        void StartProgressBar()
        {
            _startButtonIsDisabled = true;
            _stopButtonIsDisabled = false;
            _timerStartTime = DateTime.Now;
    
            // reset the progress bar ==================
            _counter = -1;
            ResetProgressBar();
            InvokeAsync(StateHasChanged); 
    
            _timer = new Timer(TimerCallback, null, 0, (int)_t1 * 1000);
        }
    
        
        void ResetProgressBar()
        {
            _progressPercent = 100;
            _progressColor = "green";
            _transition = "none";
        }
    
    
        void StopProgressBar()
        {
            _startButtonIsDisabled = false;
            _stopButtonIsDisabled = true;
            _timer.Dispose(); // Stop the timer
            var ts = DateTime.Now - _timerStartTime;
            _progressPercent = (int)(100 * ( 1 - ts.TotalMilliseconds / (_tend*1_000)));
            _transition = "none";
        }
    
    
    }

 

Answer №1

A bug was discovered in the function StartProgressBar(), specifically originating from the code snippet provided below:

ResetProgressBar();
InvokeAsync(StateHasChanged);     
_timer = new Timer(TimerCallback, null, 0, (int)_t1 * 1000);

It was observed that on mobile devices, the initialization of the new Timer was happening prior to the completion of ResetProgressBar() and InvokeAsync(StateHasChanged).

To address this issue, a dueTime parameter needed to be added during the timer instantiation as shown here: _timer = new Timer(TimerCallback, null, 5, (int)_t1 * 1000); to introduce a delay of 5 ms in this scenario.

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

Enhancing email design in a Bootstrap 5 form

Hello everyone, I'm taking my first dive into Bootstrap and seeking some guidance. If this topic has already been discussed, kindly point me in the right direction. My goal is to develop a Bootstrap 5 form that can function on a Raspberry Pi without ...

What is the process for modifying the headers of a post request in Express when

I have a Node/Express application and I'm looking to incorporate an image upload feature into an order form. While I can successfully use the action link in the HTML form to perform a POST request, I also want my JavaScript file associated with this ...

A Guide on Adding Excel-Like Filtering Functionality to AngularJS

I am struggling to implement a simple Excel-like filter for a table using AngularJS (v.1). I have shared my code snippet below and would appreciate any help to show filtered data in the table after checking a checkbox and clicking on the OK button. I have ...

Retrieve the offsetTop value of an accordion following its collapse

My goal is to determine the exact position of a clicked accordion title after it has been clicked. The issue I am facing is that the movement of Bootstrap's accordion collapse and my jQuery function are triggering almost simultaneously. As a result, ...

Restricting Options in jQuery/ajax选择列表

Although there are similar questions posted, my specific issue is that I am unsure of where to place certain information. My goal is to restrict the number of items fetched from a list within the script provided below. The actual script functions correct ...

When incorporating CSS with Bootstrap, it may not always have a visible impact on the

As a newcomer to using Bootstrap, I recently created a webpage with Bootstrap3. However, I encountered an issue when trying to apply two classes to the same element, as the CSS was not being applied: Here is the HTML code snippet: <div class="col-md-4 ...

What is the best way to verify an email input and clear it after it has been submitted?

My form is currently facing two issues: 1. The email field is accepting any text, but it should only accept valid email addresses. 2. After successful submission, I need the fields to be reset to blank. You can check out the demo site here (click the " ...

What is the best way to add or delete data when specific radio buttons are chosen?

Hey there, I'm facing an issue where the data is being appended regardless of which radio button is selected. Can someone help me with a solution on how to properly add and remove data based on the selected radio button? $( document ).ready(functio ...

What is the most efficient way to display a dynamic alphabetical order list?

sample code: <table width="100%" cellspacing="0" cellpadding="0" border="0" style="line-height: 1.7;"> <tbody> <?php $this->db->select('*'); $this->db->from('cm_options'); ...

Send the chosen value from a dropdown menu to a PHP script

<style type="text/css"> form select { display:none; } form select.active { display:block; } </style> <script type="text/javascript"> window.onload = function () { var allElem = document. ...

Creating a secondary ordered list with ongoing points - a step-by-step guide

Is there a way to create an unordered list with a continued ordered second level that automatically counts the points? I know about using <ol start="Previous number+1">, but is it possible for the list to continue numbering on its own? The ...

Struggling to align text to the far left within a text area using Bootstrap

When I accidentally place my cursor earlier in the text area, it starts writing from that point, leaving some unwanted spaces at the beginning. I prefer it to start from the extreme left, similar to how it behaves in input boxes. Below is the code snippet ...

Changing the appearance of the navigation bar

<!-- Bootstrap 5.0.x library --> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c5e5353484f484e5d4c7c09120c120e">[email protected]</a>/dist/css/bootstrap.min.css" ...

My footer is having trouble displaying social media icons properly

I'm new to coding and I'm trying to create a dark footer, dark navbar, and some carousel content. Unfortunately, the social media icons and copyright symbol in my footer are only half visible. How can I resolve this issue? Below is the code snip ...

Creating a rhombus or parallelogram on a canvas can be easily achieved by following these simple

I'm new to working with the canvas element and I want to create some shapes on it. Can someone provide me with guidance on how to draw a Rhombus or Parallelogram on a canvas? Something similar to what is shown in this image: https://i.stack.imgur.c ...

Using Django to upload an XML document via an HTML form

When working with Django framework and python scripts to validate XML files, my usual approach involves parsing the XML file located at a specified location using the following code: import xml.etree.ElementTree as ET tree = ET.parse('config.xml&apos ...

CSS does not appear to be showing on the banner

I am struggling to get my banner to display correctly using the CSS code below. #banner { background-image: url(../Images/banner.png); background-repeat: no-repeat; background-size: cover; border: 5px solid #dedede; height: 200px; } ...

What is causing the width discrepancy in my header section on mobile devices?

Help needed with website responsiveness issue! The site works fine on most screen sizes, but when it reaches around 414px in width, the intro section becomes too wide for the screen. Any ideas on what could be causing this problem? html: <nav id="m ...

Is there still excess top padding or margin in Firefox after clearing the default reset for the <ul> element?

My floated horizontal list is looking great on IE and Opera, but for some reason, Firefox is adding extra padding or margin at the top. I'm not sure how to fix it. Everything was fine until I had to add a display:inline to an image link next to it to ...

Why isn't the z-index functioning properly in Vue.js styling?

I am currently developing a simple component that displays four steps, indicating the current step and allowing users to navigate through them. I want to create a design where these numbers are enclosed within circles that are evenly distributed in a line ...