몇 가지 다른 기능을 실행하고 내 웹 페이지에 콘텐츠를로드 한 후 일련의 기능을 실행하려고합니다. 나는 setTimeOuts
을 사용했으나 연결이 지연 될 수 있다는 점을 감안할 때 잘못된 실행이라고 생각합니다.이 경우 연결이 잘못 실행됩니다. setTimeOuts
을 사용하지 않고 함수를 순차적으로 어떻게 수행합니까?다른 기능을 완료 한 후에 Javascript 기능을 시작하는 방법은 무엇입니까?
avgEmotion
및 avgSentiment
기능은 newsApiLeft()
및 newsApiRight()
기능을 통해 한 번에 일련의 목록은 웹 페이지에 생성되어 실행되어 있습니다.
미리 감사드립니다. 당신이 Left
및 Right
에 동일한 콜백을 사용하는 경우에는 콜백을 절약 할 수있다,
function newsApiLeft(callback) {
// Load stuff
// Then call the callback; the code you want
// to run after the loading is complete
callback();
}
newsApiLeft(function() {
avgEmotion();
avgSentiment();
});
모두를 위해 그것을 사용 :
button.addEventListener("click", function() {
var filter = document.getElementById("news_cat");
var filterSource = document.getElementById("news_source");
var category = filter.value;
var source = filterSource.value;
params.sources = source;
params.q = category;
parameters.q = category;
var filterL = document.getElementById("news_sourceL");
var sourceL = filterL.value;
parameters.sources = sourceL;
showContent();
clearTextRight();
clearTextLeft();
newsApiLeft();
newsApiRight();
setTimeout(avgEmotionR, 2000);
setTimeout(avgEmotionL, 2000);
setTimeout(avgSentiment, 2000);
})
당신이 콜백을 사용할 수 있지만, 당신은 그들과 함께 편안하지 않은 경우 Douglas Crockford의 RQ https://github.com/douglascrockford/RQ –
에서'newsApiLeft'와'newsApiRight'가 반환하는 것은 무엇입니까? 나는 그들이 비동기 함수 같아요. 아무것도 리턴하지 않고 매개 변수를 수신하지 않는 함수는 잘못된 코드의 명확한 표시입니다. – HMR