2016-12-07 3 views
-1

Javascript/jQuery에서 POST 요청을 만들고 해당 변수를 setTimeout 시간으로 사용하는 방법이 있습니까?비동기 반환 데이터

$.post('file.php', { action: "gettimeout"}, 
function(time){ 
    return time; 
}); 
setInterval(function(){ alert("Hello"); }, time); 

내가하려는 일입니다.

현재 할 수는 있지만 동기식으로 수행하고 있습니다.

var time = $.ajax({ 
    type: 'POST', 
    url: 'file.php', 
    data: {action: "gettimeout"}, 
    async: false 
}); 
return Number(time.responseText); 
setInterval(function(){ alert("Hello"); }, time); 
+1

콜백을 사용해야합니다. 이 답변을보십시오 : http://stackoverflow.com/a/14220323/1056133 – jayantS

답변

1

은 그냥 아약스 콜백 함수 내에서 setInterval (또는 setTimeout)에 전화를 넣어 :

$.post('file.php', { action: "gettimeout"}, function(time){ 
    setInterval(function(){ alert("Hello"); }, time); 
}); 

NB를 : 아약스 콜백 함수의 값을 반환에 대한 의미가 없습니다. How do I return the response from an asynchronous call을 참조하십시오.