私はこのコードを持っています:
us.isConnected()
.then(function (msg) { er.msg = msg }, function (msg) { er.msg = msg });
$interval(function () {
us.isConnected()
.then(function (msg) { er.msg = msg }, function (msg) { er.msg = msg });
}, 20 * 1000);
接続を確認してからメッセージを出力します。
このコードを単純化して再帰的にする方法はありますか?そうすれば、.then部分を複数回コーディングする必要がなくなりますか?
以前に実行されたリクエストが終了するのを待たずに、複数回リクエストを実行する可能性があること$timeout
に依存する代わりに使用できます。$intervals
isConnected()
var promise;
// execute testConnection()
testConnection();
function testConnection() {
// run request initially
return request().finally(function() {
// runs the request recursively
// and assign the timeout's promise
// if you need to cancel the recursion
return (promise = $timeout(request, 20 * 1000));
});
}
// request if ui is connected
function request() {
return ui.isConnected()
.then(setErr, setErr);
}
// ser `er` object
function serErr(msg) {
er.msg = msg;
}
// cancels the recursive timeout
function cancel() {
$timeout.cancel(promise);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加