循环中的JavaScript异步调用

时间:2017-04-18 05:22:08

标签: javascript jquery loops asynchronous

我在javascript中面临异步调用问题,错误(不是预期的)值传递给函数。见伪代码:

    i=0;
    while(i<10){  
        var obj= {something, i};    
        getcontent(obj); //( <--- getcontent is async function/problem)      
     i++;
    }

所有getcontent异步调用都使用最后一个i = 9值,这不是我想要实现的 我应该如何处理这样的异步调用并在每次调用中获得正确/传递正确的值?

3 个答案:

答案 0 :(得分:0)

<script>
var i = 0;
while (i < 5) {
setTimeout(function() { 
 getcontent()
}, 2000);  
    i++;
}
function getcontent(){
alert()
}
</script>

希望它能帮到你

答案 1 :(得分:0)

这个怎么样:

async function loop(){
    var i: number = 0;
    while (i < 10) {
        (async function () {
            var obj = { "@": "fd", "#": i };
            await getcontent(obj); //( <--- getcontent is async function/problem)      
            i++;
        })();
    }
}

答案 2 :(得分:0)

我认为这就是你要找的东西~~

&#13;
&#13;
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.useProperties=false
org.quartz.jobStore.dataSource=myDS
org.quartz.dataSource.myDS.driver =oracle.jdbc.OracleDriver
org.quartz.dataSource.myDS.URL = jdbc:oracle:thin:@example:1521:db
org.quartz.dataSource.myDS.user = user
org.quartz.dataSource.myDS.password = password
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery = select 1 from dual
org.quartz.jobStore.isClustered=false
org.quartz.jobStore.tablePrefix = DPPA.QUARTZ_
org.quartz.threadPool.threadCount=1
org.quartz.scheduler.skipUpdateCheck=true
org.quartz.plugin.jobHistory.class=id.co.fifgroup.dpa.batch.BatchHistoryListener
&#13;
&#13;
&#13;