本文介绍了Python while循环转换为Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换以下内容:

while True:
    # do something
    time.sleep(2)

进入javascript?

into javascript?

推荐答案

你不会,因为JavaScript没有睡觉 - 它是同步和基于事件的。但是,您可以通过和:

You would not, as JavaScript does not sleep - it is synchronous and event-based. Yet, you can schedule functions to be executed later in time via setTimeout and setInterval:

var timerid = setInterval(function() {
    // do something
    // instead of "break", you'd use "clearTimeout(timerid)"
}, 2000);






对于你的ajax进度条,我建议以下不严格请求每个2s,但等待它们返回:


For your ajax progress bar, I'd recommend the following which does not fire requests strictly each 2s, but waits for them to return:

function getUpdate() {
    myAjax(…, function onAjaxSuccess(result) { // an async event as well
        // show(result)
        if (!result.end)
            setTimeout(getUpdate, 2000);
    });
}
getUpdate();

这篇关于Python while循环转换为Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:58
查看更多