Chaining methods with delays in javascript

I'm attempting to make this piece of code more efficient.

(function() { sc = angular.element('tbody').scope(); sc.draft.resetRoster(); sc.$apply(); setTimeout(function() { sc.draft.rosterAdd({id: "12921", salary: 10600, position: "P"});sc.draft.rosterAdd({id: "12123", salary: 2900, position: "C"});sc.draft.rosterAdd({id: "5435", salary: 3800, position: "1B"});sc.draft.rosterAdd({id: "12562", salary: 2400, position: "2B"});sc.draft.rosterAdd({id: "38321", salary: 3100, position: "3B"});sc.draft.rosterAdd({id: "6319", salary: 2400, position: "SS"});sc.draft.rosterAdd({id: "5204", salary: 3200, position: "OF"});sc.draft.rosterAdd({id: "5222", salary: 2500, position: "OF"});sc.draft.rosterAdd({id: "12462", salary: 2600, position: "OF"}); }, 3000); setTimeout(function() { document.querySelector('#enterButton').click(); }, 6000); })();

I'm not entirely familiar with Angular (actually not at all). I'm trying to create a client-side script that interacts with Angular. There's 5 total statements here that I'm trying to condense into as little code as possible. Before the first setTimeout gets triggered in 3,000ms, I need this to run: sc = angular.element('tbody').scope(); sc.draft.resetRoster(); sc.$apply();

After 3,000ms, the first setTimeout runs. Roughly 6,000ms (assuming there's roughly a ~3,000ms gap between the first setTimeout and second setTimeout) I need the second setTimeout to execute.

Is there a better way to put together this function? Less code? Better sequencing of how I'm handling setTimeout in general?

-------------Problems Reply------------

Instead of setTimeout use angular service $timeout which functions similarly but returns a promise that will be resolved after the specified amount of time has elapsed, and also calls $apply. So in your case:

$timeout(function () { ... }, 3000).then(function () {
//after the first timeout we set another one and return its promise
$timeout(function () { ... }, 3000);
});

This way you get rid off the mental arithmetic and you can just set the time you want to wait after the last timeout has elapsed, without nesting timeout functions.

Category:javascript Views:5 Time:2019-03-09

Related post

  • Enabling chained method using ".delay" 2010-08-06

    all. I'd like to know how to realize this method-chain style: var foo = function(){}; foo.prototype = { say : function(s){ alert(s); return this; }, delay : function(ms){ var _this = this; setTimeout(function(){ return _this; }, ms); return this; //

  • Add method with parameters to javascript object 2011-09-21

    I am currently in process of porting one of my java applet games to javascript+html5. I have never done object oriented javascript before and this prototype based OO stuff is confusing me a lot. I tried to do a straightforward port from java but am h

  • Stubbing Chained Methods with Rspec 2009-03-20

    I want to call a named_scope that will only return one record, but the named_scope returns an array, that's not a big deal as I can just chain it with .first: Model.named_scope(param).first and this works, what I am struggling with is how to stub the

  • Unit testing a method with delay in Objective-C 2011-12-11

    I'd like to write black-box unit tests for a method that calls another one with a delay. It kinda looks like this: - (void) doSomething { // Do something [self performSelector:@selector(doSomethingLater) withObject:nil afterDelay:kDelay]; } - (void)

  • Call apply method with arguments in JavaScript 2011-10-06

    I wanted to invoke a function using the javascript apply() method. This works fine if the function has no arguments. i.e. function test() { console.log(this); } body = document.getElementsByTagName("body")[0]; // shortcut to body element test.apply(b

  • how to call a method of multiple arguments with delay 2012-03-09

    I'm trying to call a method after some delay. I know there is a solution for that: [self performSelector:@selector(myMethod) withObject:nil afterDelay:delay]; I saw this question and Documentation But my question is: How can I call a method that take

  • error when calling applet method with javascript 2010-07-25

    i want to read network card identity of a machine using applet that only works on jre 1.6 & above. even applet loaded sucessfully on page loading , i was not able to call Applet methods with javascript. when i checked the applet functions by call

  • Chaining methods in Javascript 2010-11-09

    I want to chain methods in Javascript (using Node.js). However, I encountered this error: var User = { 'deletes': function() { console.log('deletes'); return this; }, 'file': function(filename) { console.log('files'); } }; User.deletes.file(); node.j

  • How can i use jQuery's .delay() method with .hide() or show()? 2011-01-12

    Possible Duplicate: How can I use delay() with show() and hide() in Jquery How to use jQuery's .delay() method with .hide() or show()? the following doesn'tworks: $("#element").val("").delay(250).hide(); but this does: $("#element").val("").delay(250

  • How to build chained methods like - should_receive(:something).with(:params, values).and_return(:something_else) 2011-06-02

    I was trying to make my code a bit smaller building a method that can look like the should_receive from RSpec, the case here is that I'm testing a state machine and I have several methods with code like this: context "State is unknown" do before do @

  • Which method is called during Constructor chain execution with an overriding method 2012-01-28

    Which method is called during Constructor chain execution with an overriding method? Given the following two classes I need to know which setGear method would be called when creating a MountainBike object. My real project has nothing to do with bicyc

  • Is it possible to use "find" method with "javascript" query with pymongo? 2012-02-09

    With Mongo, it is OK with the following: > db.posts.find("this.text.indexOf('Hello') > 0") But with pymongo, when executing the following: for post in db.posts.find("this.text.indexOf('Hello') > 0"): print post['text'] the error occurred. I

  • javascript show in cycle with delay 2010-07-19

    Simple example: for (var i = 0; i < 10; ++i) { console.log(i); // <--- should be show with delay in 300ms } Simple setTimeout using of course doesn't work... I guess there's should be using closures.. --------------Solutions------------- Should

  • Http Option Method with Javascript request 2011-12-01

    I use backbone.js' s model. When i save the model, it sends HTTP OPTIONS method to server-side on firefox, but sends HTTP POST method with safari. I know it is not an issue about backbone.js, it is about CORS. I will just check if method, GET, POST,

  • Put a Delay in Javascript 2009-07-26

    I need to add delay about 100 miliseconds to my javascipt but I don't want to use thesetTimeout function of the window object and I don't want to use a busy loop. Does anyone have any suggestions? --------------Solutions------------- Unfortunately, s

  • Delay "no Javascript" warning from showing? 2011-04-23

    I'm using the "noscript.js" file (detailed here: http://dev.opera.com/articles/view/replacing-noscript-with-accessible-un/) to display a large (100+ px tall) "Javascript disabled" warning. However, I get a flash of the Javascript warning before the r

  • Problem with inserting mutiple javascript functions with Greasemonkey using unsafeWindow 2011-05-19

    The problem is that the functions do not seem to be able to see each other. For Example: unsafeWindow.helloworld = function() { alert('Hello world!'); helloworld2();//fails with error saying it does not exist } unsafeWindow.helloworld2 = function() {

  • What happens in WCF to methods with IsOneWay=true at application termination 2008-12-06

    I have a client application that once in while notifies about its progress a service. The method call to the service is marked with IsOneWay=true, because the notification doesn't need any return value and I don't want to delay. The client may notify

  • how to get parameter like mysite.com/myaction/#someanchor with php and javascript 2009-04-03

    For example I hide comments by default. So if I find #comments I will be know that I need to show them up. Is that possible to catch with php or javascript on page load? --------------Solutions------------- With JavaScript you can access the location

  • Methods for dynamically building JavaScript within an ASPX page? 2009-09-11

    I have a page that is referenced via a <script> tag from a page on another site. In the script src, I pass in the form I want my script to build (from a db table), and the div where the dynamically built form should go. The calling page looks s

  • How to chain method on a newly created object? 2010-02-02

    I would like to know whether there's a way to chain methods on a newly created object in PHP? Something like: class Foo { public function xyz() { ... return $this; } } $my_foo = new Foo()->xyz(); Anyone know of a way to achieve this? -------------

  • Custom Object calling Methods with setTimeout loses scope 2010-02-25

    I'm having an issue with building a Javascript object, and calling methods within that object using setTimeout. I've tried various workarounds, but always during the second part of my loop the scope becomes the window object rather than my custom obj

  • Calling ASP.NET web service function via GET method with jQuery 2010-04-26

    I'm trying to call web service function via GET method using jQuery, but having a problem. This is a web service code: [WebService(Namespace = "http://something.com/samples")] [ScriptService] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_

  • If the "with" statement in Javascript creates a new scope, why does the following code not work as expected? (the closure doesn't contain the new "x" in new scope each time) 2010-04-30

    If the with statement in Javascript creates a new scope, shouldn't clicking on the links show a different x which are in different scopes? It doesn't... <a href="#" id="link1">ha link 1</a> <a href="#" id="link2">ha link 2</a>

  • How do I imply code contracts of chained methods to avoid superfluous checks while chaining? 2010-05-02

    I'm using Code Contracts in C# 4.0. I'm applying the usual static method chaining to simulate optional parameters (I know C# 4.0 supports optional parameters but I really don't want to use them). The thing is that my contract requirements are execute

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.190 (s). 11 q(s)