angularjs - Chaining methods with delays in javascript -
i'm attempting make 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 angular (actually not @ all). i'm trying create client-side script interacts angular. there's 5 total statements here i'm trying condense little code possible. before first settimeout
gets triggered in 3,000ms, need run: sc = angular.element('tbody').scope(); sc.draft.resetroster(); sc.$apply();
after 3,000ms, first settimeout
runs. 6,000ms (assuming there's ~3,000ms gap between first settimeout
, second settimeout
) need second settimeout
execute.
is there better way put function? less code? better sequencing of how i'm handling settimeout
in general?
instead of settimeout
use angular service $timeout
functions returns promise
resolved after specified amount of time has elapsed, , calls $apply
. in case:
$timeout(function () { ... }, 3000).then(function () { //after first timeout set 1 , return promise $timeout(function () { ... }, 3000); });
this way rid off mental arithmetic , can set time want wait after last timeout has elapsed, without nesting timeout functions.
Comments
Post a Comment