logo
down
shadow

Waiting for function with a setTimeout to complete without tampering with that function


Waiting for function with a setTimeout to complete without tampering with that function

By : Ganesh Dhanawde
Date : November 22 2020, 02:59 PM
this one helps. You can monkey-patch setTimeout because it's a property of the global object:
code :
function trapTimeout() {
    return new Promise(function(resolve, reject) {
        var setTimeout = window.setTimeout;
        window.setTimeout = function(cb, delay) {
            var args = [].slice.call(arguments, 2);
            setTimeout.call(window, function() {
                window.setTimeout = setTimeout;
                cb.apply(null, args);
                resolve();
            }, delay, ctx);
        }
    });
}

var trap = trapTimeout();
foo();
trap.then(function() { console.log('really done') });


Share : facebook icon twitter icon
setTimeout won't wait, even when waiting to call a function

setTimeout won't wait, even when waiting to call a function


By : mazda_2014
Date : March 29 2020, 07:55 AM
Hope that helps Started learning javascript yesterday at CodeAcademy and decided I could actually write code. Just kidding. Looked around online, found people with a similar learning curve but none of the answers out there fixed my issue.
code :
setTimeout(slideIn(xslide), 20);
setTimeout(slideIn, 20);
setTimeout(function (){
    slideIn(xslide);
}, 20);
setTimeout(function slideTimeout(){
    slideIn(xslide);
}, 20);
Callback function isn't waiting until the first function is complete. How do I fix/work around this?

Callback function isn't waiting until the first function is complete. How do I fix/work around this?


By : sudhir
Date : March 29 2020, 07:55 AM
Hope this helps The problem is that .hide() is running for all matched elements and then running the callback even when the matched element was not visible. By using the .filter() method you can ensure that the `.hide()' method, and thus the callback, only get fired once. This means that the code gets bulked out as you need to catch the case when nothing is shown already, but it does work quite well.
An example of this is
code :
$('a.red').click(function () {
    var elements = $("#blue, #green, #yellow").filter(":visible");
    if (elements[0]) {
        elements.fadeOut(function () {
            $("#red").fadeIn();
        });
    } else {
        $("#red").fadeIn();
    }
});
setTimeout firing function straight away and not waiting

setTimeout firing function straight away and not waiting


By : test5000
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Your function getNegativeErrs() is executing right away and is using its return value as the callback.
The right way:
code :
window.setTimeout(function() {
    getNegativeErrs(errors)
    }, 5000);

  function getNegativeErrs(ers) {
    for( i in errors ) {
      var errobj = getByValue(dataLayer, errors[i]);
      if(!errobj) {
        dataLayer.push({
          'event': 'negative_errors',
          'unseen': errors[i],
          'module': 'registration'
        })
      }
    }
  }
JS node, waiting for function to complete before executing second function

JS node, waiting for function to complete before executing second function


By : Lởie Sayàng Mỹ
Date : March 29 2020, 07:55 AM
I wish this help you I am trying to execute firstFunction() which parses a csv file and returns some data, and pass that to secondFunction() to then perform some operations. I've tested both independently and they both work as intended, but I can't get the secondFunction() to execute after firstFunction() is complete. This results in 'data' being undefined at time of execution. , You can return a promise from the firstFunction
code :
function sometestFunction() {
  return Math.random();
}

function secondFunction(data) {
  console.log('data received from first function ' + data)
}

function firstFunction() {
  return Promise.resolve(sometestFunction())
}

firstFunction().then(function(data) {
  secondFunction(data);
})
async function parentFunc() {
  let x = await firstFunction();
  secondFunction(x);
}



function firstFunction() {
  return new Promise(function(resolve) {
    setTimeout(() => {
      resolve([1, 2, 3, 4, 5, 6]);
    }, 3000)
  })
}

function secondFunction(data) {
  for (var i = 0; i < data.length; i++) {
    console.log(data[i])
  }
}

parentFunc();
In angular function not waiting to complete first function execution

In angular function not waiting to complete first function execution


By : Frangky Al Bholoet
Date : March 29 2020, 07:55 AM
I wish this helpful for you This is expected due to asynchronous nature of execution. You should use callbacks to avoid this problem.
Related Posts Related Posts :
  • How to multiply each element in an array with a different number (javascript)
  • Constructing a query string for JQuery getJSON for a public API GET request
  • Server side error generating RSS - what to return back to user/RSS reader?
  • ES6 code on Nodejs 4.x : Is V8 4.5 a native JS engine that runs ES6 directly?
  • How to only get new data without existing data from a Firebase?
  • How to set default option d3.js
  • using enum in Javascript for month number against name
  • React component only changes state on second onClick event
  • JavaScript text area: add line break
  • Unobtrusive jQuery unobtrusive validation not showing error spans
  • get the location for objects in a firebase array?
  • Multiple addEventListener not storing unique callbacks
  • JQuery, variable inside function not recognized by action
  • User input in Javascript
  • JavaScript Regex Remove Content from String
  • Is there a way to apply a mask on keydown? (JQuery)
  • Redirect A Live Website Visitor Without Them Refreshing or Clicking anything?
  • Exclude HTML tags when translating with Google Translate API
  • Posting to a ColdFusion function from angularJS
  • How to nullify the fadeout?
  • JS not updating on enquire.js breakpoints
  • AngularFire Check if item of same title exists
  • add names to objects based on object value
  • When do you use "class" versus "id"?
  • Get content of loaded SVG file
  • How to find size of an image file from canvas?
  • ui-sref="articles.view({articleId: article._id})" meaning?
  • How to structure Angular with Highcharts and lots of dynamic data
  • Why does a component class needed to be reopened to specify positional params?
  • Can I disable sorting in dgrid for performance gain?
  • Which tools (that are not plugins) can be used to create animation which can be manipulated at runtime?
  • Boostrap carousel not working
  • Ember controllers in nested routes
  • How to reload ng-include in AngularJS?
  • Add enter on event in javascript
  • How to set different background color for each repeated column of RadioButtonList
  • Angular filter data by date
  • Showing multiple info boxes in Google Maps when pulling from MySQL
  • Cordova event DeviceReady don't work on all pages
  • JavaScript document object lastModified using toLocale string
  • TypeError: $(...).autocomplete is not a function $("#partipnt-name").autocomplete({
  • Javascript isnan issue
  • Change variance on mouse movement
  • Highcharts - set xAxis range when using xAxis Categories?
  • Typeahead 0.11.1 Mouse over and keyboard conflict when viewing dropdown search results
  • How to use data from $http.get after page load in Vue.js?
  • How to test angular promise with Jasmine
  • Conditionally chain functions in JavaScript
  • Using QueryBuilder and getting "QueryBuilder is not defined" error
  • slideToggle with bounce effect not working
  • AngularJS binding two variables together without being told to do so
  • Anchor doesn't work
  • set different values for drop down list if check box checked or unchecked
  • Fullscreen slide-toggle with cookie
  • Set variable if input field contains text
  • signin with Linkedin code give me html validation error
  • how to call a jquery function from a form
  • iron router reload specific header element
  • .submit function not running when invalid with kendo validator
  • How to use bind() in JS object init
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co