What is the solution to pausing $ionicLoading when a promise is being fulfilled?

I created an ionicLoading with a function called startNow() to navigate from original-page to my-next-page.

The first time, I simply invoke startNow() which takes me directly to my-next-page after the $timeout() service is triggered.

However, I encountered a problem:

In my myLoadingTemplate.html, there is a cancel button that also triggers the startNow(true) function. The issue arises when I click this cancel button during loading - the page redirects back to the original-page. Nevertheless, after 2500ms, the page then correctly navigates to my-next-page.

How can I resolve this issue?

$scope.startNow = function(is_force)
{
     if(is_force===true)
     {
         $state.go('orignal-page');
         $ionicLoading.hide();
     }else
     {
         $ionicLoading.show({
             templateUrl: "myLoadingTemplate.html",
             nBackdrop: false
           }).then(function(){
                   $timeout(function(){ 

                      $state.go('my-next-page');
                      $ionicLoading.hide();

                   },2500);
         });
     }
}

Answer №1

It seems like your main concern is preventing the delayed state change after the timeout when cancelling. One way to achieve this is by canceling the timeout itself. The $timeout function returns a promise that can be saved and used to cancel the timeout when needed, such as when clicking a cancel button.

I have made some modifications to your code below in hopes that it resolves the issue you are facing:

Correction: I mistakenly mentioned that $ionicLoading.show returns a promise, which is actually incorrect in this case. I have fixed this in the code example provided below. Additionally, I changed $scope.transitionTimeout to $rootScope.transitionTimeout. While using $rootScope is not the cleanest solution, it was the quickest way to demonstrate a working example. Since it's not possible to pass a scope to the $ionicLoading service directly, adding a controller to the loading template creates a new scope where the transitionTimeout promise is not defined.

A better approach would be to create separate controllers for the view and the loading template, with a service handling communication between them and storing the promise.

However, the following code offers a simple and functional solution. You can also test it out here:

$scope.startNow = function(is_force)
{
     if(is_force === true)
     {
         // Cancel the timeout using the reference
         // Note: Using $rootScope may not be the best practice here
         //       This is just to quickly address the issue
         //       A more efficient solution would involve implementing a LoadingService
         //       to store the transition timeout promise uniquely
         $timeout.cancel($rootScope.transitionTimeout);

         $state.go('original-page');
         $ionicLoading.hide();
     } else
     {
         $ionicLoading.show({
           templateUrl: "myLoadingTemplate.html",
           nBackdrop: false
         })

         // Save the promise here
         $rootScope.transitionTimeout = $timeout(function(){ 
           $state.go('my-next-page');
           $ionicLoading.hide();
         });
     }
}

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 Typescript and Webpack - Make sure you have the right loader set up to handle this specific file type, as at the moment there are no loaders configured to process

I've encountered an issue while setting up Webpack for Typescript and React. Upon executing the NPM script: webpack serves ./webpack/webpack.config.ts --open, an error surfaced in the console: https://i.sstatic.net/xqpUV.jpg Below are the details of ...

What is the best way to ensure a textarea remains expanded when the parent element is in focus?

I have successfully implemented a textarea box that expands upon click and retracts when it loses focus. However, I am facing an issue where if the textbox is already in expanded form, I want it to stay expanded if the user clicks anywhere within the cont ...

struggling with the predictive text feature, need some assistance

Currently, I am utilizing the material-table library and I have made the decision to incorporate a feature similar to Google's "typeahead" functionality. Here is an example of what I am trying to achieve: https://i.sstatic.net/r6jCd.png In order to ...

Creating an AngularJS directive that includes a dynamic template and transclude functionality

I am currently working on adding a dynamic template, but I have encountered an issue with transclude. Here is the dynamic template function I am using: var createTemplate = function(contentType){ var template = '<button style="cursor: point ...

Utilize JSON to populate data in a switch case scenario

I am attempting to implement switch cases using JSON. How can I load the data into the switch statement? Here is the desired output: $(".custom-menu li").click(function(){ switch($(this).attr("data-action")) { case "Science": $('.abc:e ...

Dealing with local JSON data asynchronously using ReactJS

Within my local workspace environment, there lies a valuable data.json file. Our application operates solely on the client side. I find myself torn between two potential methods of data consumption: import data from './data.json' // and start w ...

Forward users to specific date and time on Everwebinar link

Is there a way to automatically redirect visitors of my custom Everwebinar confirmation page (on my domain) to a specific URL at a set date and time that is included in the confirmation page URL? Here is an example of what the confirmation page URL looks ...

Implementing an active state for the initial item in jCarousel Lite

Is there a way to add a 'class="active"' state to the first element in the list when initializing jCarousel Lite without modifying the plugin? This is my current setup - $('#viewport').jCarouselLite({ speed: 500, visible: 3, ...

Resolving promises in a sequential manner can lead to encountering errors along

Currently, I've been attempting to iterate through an array of IDs and pass them into a function (this.handleTransfer) that makes API calls. My goal is for the next iteration to only proceed when a response is received from the previous one. In my que ...

What is the reason for the global impact of CSS-Modules on html elements?

There is a file named mycomponent.module.css button { width: 10vw; min-width: 150px; } It seems like this CSS is being applied globally instead of just to the specific component where I imported it. Could it be that CSS modules only work on class ...

Leveraging React Router v5 along with Code Splitting and Server Side Rendering for optimized Data Prefetching

For a current project, I am using react-router v3 specifically for server-side rendering with data prefetching. The most efficient way to achieve this is by maintaining a centralized route configuration in either an object or an array, and iterating throug ...

The Disable Button is malfunctioning as the text is deleted but the button remains enabled

One issue I am facing is that even after removing the numbers from the textboxes, the submit button remains enabled. Initially, when I enter inputs in the textbox, it enables the button, but even after removing a number, the button stays enabled. This is ...

Retrieve Content from a Different Web Page Using Ajax

I recently received permission from another website to pull their RSS feeds, and now I'm trying to figure out what to write in TAG1 and TAG2. This is the specific issue I'm facing: Here is the HTML code (it's an ajaxed page) <!doctype ht ...

When AJAX is invoked, the HTML content fails to display within a div

The following is the ajax script I am currently utilizing: mypage.php $(".sales_anchor").click(function(e) { e.preventDefault(); var store_username = $("#storeUsername").val(); var store_id = $("#storeID").val(); var sale_id = $(this).a ...

Incorporate Subtitles into Your Website Using JWPlayer

I want to incorporate Video Captions similar to those seen on Lynda.com, for example at The captions should synchronize with the player and also appear in a separate block of HTML below the player. I am using JWPlayer for my video and have successfully in ...

initiating a submission upon the occurrence of an onchange event on an input field of type "file"

I have encountered an issue while trying to submit a form using the onchange event of an input element with type file. The problem is that it submits an empty form even when a file has been chosen. Here is the code snippet: var form = document.createElem ...

Executing Angular.js command to activate a separate directive

Seeking advice on implementing a generic toggle feature between directives. The goal is to have a directive containing a template that remains hidden until triggered by an event from another directive. Any ideas on how to connect these two? Appreciate any ...

Creating a Cubic Bezier Curve connecting two points within a 3D sphere using three.js

I'm currently working on a project where the user can click on two points on a sphere and I want to connect these points with a line along the surface of the sphere, following the great circle path. I have managed to obtain the coordinates of the sele ...

With Ionic, you can use one single codebase for both iPad and iPhone

I currently have a complete app developed using ionic and angularjs that is functioning well on iPads and Android devices. Now we are looking to launch it for iPhones and Android smartphones with some design modifications. Is there a method to achieve th ...

Setting bootstrap datetimepicker values dynamically from a variable

I'm trying to utilize the inline bootstrap datetimepicker from At the moment, it opens in a popup window after the user selects a date/time from a table on another page. My goal is to pass the date/time from that page into datetimepicker and have it ...