套接字连接后等待 angular 呈现元素
Wait for angular to render an element after socket connect
我想在有成功的套接字 connect
和 angular
呈现元素时触发函数。
Ex. <canvas id="{{currentUserId}}"></canvas>
我想在我的代码中遵循以下内容:
socket.on('connect',function(){
until(<above canvas is present>)
callMe();
});
使用 async 库找到解决方案:
socket.on('connect',function(){
async.until(
function(){
return $('#canvasId').length > 0;
},
async.ensureAsync(
function(retry){
console.log('trying');
retry(null,null);
}
),
function(err,result){
console.log('rendered');
callMe();
}
);
});
解释如下:
async.until(test,async.ensureAsync(fn),callback){}
test : return true/false, if returns false fn will be called and this will repeat until true is returned and on that final callback is called
fn: do some action, for my case I just wanted to wait, so I am not performing any action
async.ensureAsync(fn) : ensures that one fn is called in one tick and prevents stack overflow if there are too many attempts
callback: is called when test returns true
我想在有成功的套接字 connect
和 angular
呈现元素时触发函数。
Ex. <canvas id="{{currentUserId}}"></canvas>
我想在我的代码中遵循以下内容:
socket.on('connect',function(){
until(<above canvas is present>)
callMe();
});
使用 async 库找到解决方案:
socket.on('connect',function(){
async.until(
function(){
return $('#canvasId').length > 0;
},
async.ensureAsync(
function(retry){
console.log('trying');
retry(null,null);
}
),
function(err,result){
console.log('rendered');
callMe();
}
);
});
解释如下:
async.until(test,async.ensureAsync(fn),callback){}
test : return true/false, if returns false fn will be called and this will repeat until true is returned and on that final callback is called
fn: do some action, for my case I just wanted to wait, so I am not performing any action
async.ensureAsync(fn) : ensures that one fn is called in one tick and prevents stack overflow if there are too many attempts
callback: is called when test returns true