How can I create an Asynchronous function in Javascript?

news/2024/7/7 18:36:00

哈哈:)我的codepen 的代码笔记是:http://codepen.io/shinewaker/pen/eBwPxJ

 -------------------------------------------------------

 

84down votefavorite
39

I mean, check it out this code :

<a href="#" id="link">Link</a> <span>Moving</span> $('#link').click(function () { console.log("Enter"); $('#link').animate({ width: 200 }, 2000, function() { console.log("finished"); }); console.log("Exit"); });

as you can see in console, the "animate" function is Asynchronous, and it "fork" the flow of the event handler block code. In fact :

$('#link').click(function () { console.log("Enter"); asyncFunct(); console.log("Exit"); }); function asyncFunct() { console.log("finished"); }

follow the flow of the block code!

If I wish to create my function asyncFunct() { } with this behaviour, how can I do it with javascript/jquery? I think there is a strategy without use setTimeout() ​

 

You cannot make a truly custom asynchronous function. You'll eventually have to leverage on a technology provided natively, such as:

  • setInterval
  • setTimeout
  • requestAnimationFrame
  • XMLHttpRequest
  • WebSocket
  • Worker
  • Some HTML5 APIs such as the File API, Web Database API
  • Technologies that support onload
  • ... many others

In fact, for the animation jQuery uses setInterval.


http://lihuaxi.xjx100.cn/news/243175.html

相关文章

SSL/TLS原理详解

本文大部分整理自网络&#xff0c;相关文章请见文后参考。 关于证书授权中心CA以及数字证书等概念&#xff0c;请移步 OpenSSL 与 SSL 数字证书概念贴 &#xff0c;如果你想快速自建CA然后签发数字证书&#xff0c;请移步 基于OpenSSL自建CA和颁发SSL证书 。 SSL/TLS作为一种互…

APP自动化测试过程概述

对于Android App的自动化测试框架的使用&#xff0c;其实在很多书上面都会有说明&#xff0c;我们可以先来看一个常用的自动化测试实例&#xff0c;先不说框架&#xff0c;主要是测试用户操作的模拟、执行结果的判断&#xff0c;以便获得对测试自动化的理解与认识。 案例需求如…

jdk7新特性学习笔记

jdk7新特性学习笔记 从网络down了视频看&#xff0c;记录下学过的东西。1.二进制字面量 JDK7开始,可以用二进制来表示整数&#xff08;byte,short,int和long&#xff09;&#xff0c;语法&#xff1a;在二进制数值前面加 0b或者0B例如&#xff1a;int x 0b11112.数字字面量可以…

Linux进程ID号--Linux进程的管理与调度(三)

进程ID概述 进程ID类型 要想了解内核如何来组织和管理进程ID&#xff0c;先要知道进程ID的类型&#xff1a; 内核中进程ID的类型用pid_type来描述,它被定义在include/linux/pid.h中 enum pid_type {PIDTYPE_PID,PIDTYPE_PGID,PIDTYPE_SID,PIDTYPE_MAX };12345671234567PID 内核…

如何理解JS的单线程?

JS本质是单线程的。也就是说&#xff0c;它并不能像JAVA语言那样&#xff0c;两个线程并发执行。 但我们平时看到的JS&#xff0c;分明是可以同时运作很多任务的&#xff0c;这又是怎么回事呢? 首先&#xff0c;JS的代码&#xff0c;大致分为两类&#xff0c;同步代码和异步代…

C#组件系列——又一款日志组件:Elmah的学习和分享

前言&#xff1a;好久没动笔了&#xff0c;都有点生疏&#xff0c;12月都要接近尾声&#xff0c;可是这月连一篇的产出都没有&#xff0c;不能坏了“规矩”&#xff0c;今天还是来写一篇。最近个把月确实很忙&#xff0c;不过每天早上还是会抽空来园子里逛逛。一如既往&#xf…

vsftpd用户配置 No.2

在配置ftp虚拟用户的过程中&#xff0c;还有一种配置方式。yum -y install 安装vsftpdcp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak编辑vsftpd.conf开启下列选项&#xff1a;anonymous_enableNOlocal_enableYESwrite_enableYESlocal_umask022anon_mkdir_write_enab…

EXT.JS的PROXY放在哪里,STORE放在哪里,绝对是个技术活儿啊。

我理解的是&#xff0c;单独的STORE&#xff0c;会在应用程序开始时就加载&#xff0c; 而VIEWMODEL的STORE&#xff0c;会在VIEW加载时才开始加载。 PROXY放在STORE&#xff0c;则会在调用这个STORE的VIEW才能请求服务器数据。 如果PROXY放在MODEL时&#xff0c;则凡使用这个M…