问题描述
我真的努力在这里得到认真处理编写异步的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.
用一个例子提供):
window.setTimeout(function() {
console.log("World");
}, 1000);
console.log("Hello");
这篇关于什么是异步javascript函数的一个简单的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!