Python while循环转换为Javascript [英] Python while loop conversion to Javascript

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

问题描述


可能重复:

在Javascript中睡觉

如果我想要一个JavaScript版本的sleep()我该怎么办?

我如何转换以下内容:

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

进入javascript?

into javascript?

推荐答案

你不会,因为JavaScript没有睡觉 - 它是同步和基于事件的。但是,您可以通过 <$ c $安排稍后执行的功能。 c> setTimeout setInterval

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆