什么是异步javascript函数的一个简单的例子? [英] What is a simple example of an asynchronous javascript function?

查看:163
本文介绍了什么是异步javascript函数的一个简单的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的努力在这里得到认真处理编写异步的JavaScript。能否请您提供一个简单的JavaScript函数,它是异步的写在普通的JavaScript的一个实例(而不是使用Node.js的或JQuery的)

I am really struggling here to get to grips with writing asynchronous JavaScript. Could you please provide an example of a simple JavaScript function which is asynchronous written in plain JavaScript (and not using Node.js or JQuery)

推荐答案

JavaScript的本身是同步的,单线程的。你不能写一个异步函数;纯JS没有时间API。届时将有来自并行线程没有副作用。

JavaScript itself is synchronous and single-threaded. You cannot write an asynchronous function; plain JS has no timing API. There will be no side-effects from parallel threads.

你可以做的是使用由环境(Node.js的,的网页浏览器),让您安排异步任务提供了一些API - 使用超时,AJAX功能,FileAPI, requestAnimationFrame nextTick ,WebWorkers,DOM事件,等等。

What you can do is use some APIs provided by your environment (Node.js, Webbrowser) that allow you to schedule asynchronous tasks - using timeouts, ajax, FileAPI, requestAnimationFrame, nextTick, WebWorkers, DOM events, whatever.

用一个例子 的setTimeout (由 HTML时序API 提供):

window.setTimeout(function() {
    console.log("World");
}, 1000);
console.log("Hello");

这篇关于什么是异步javascript函数的一个简单的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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