zoukankan      html  css  js  c++  java
  • How can I create an Asynchronous function in Javascript?

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

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

    I mean, check it out this code :

    <a href="#" id="link">Link</a>
    <span>Moving</span>
    
    $('#link').click(function () {
        console.log("Enter");
        $('#link').animate({  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.

  • 相关阅读:
    位运算操作
    C 动态分配内存
    数据查询语言 DQL
    数据操纵语言 ,DML, 增删改
    Convert Sorted List to Binary Search Tree
    Longest Consecutive Sequence
    Binary Tree Postorder Traversal
    Triangle
    4Sum
    3Sum Closest
  • 原文地址:https://www.cnblogs.com/oxspirt/p/6229154.html
Copyright © 2011-2022 走看看