Giter VIP home page Giter VIP logo

Comments (5)

jirengu avatar jirengu commented on July 21, 2024 4
    buttonA.onclick = function(){
      get('/a', function(resTxt){
        console.log(resTxt)
        input.value = resTxt
      })
    }

    buttonB.onclick = function(){
      get('/b', function(resTxt){
        console.log(resTxt)
        input.value = resTxt
      })
    }


    function get(url, callback){
      
      if(!Array.isArray(get.xhrQueue)){
        get.xhrQueue = []
      }
      var xhrQueue = get.xhrQueue
      var xhr  = new XMLHttpRequest()
      xhrQueue.push(xhr)
      xhr.opts = {}
      xhr.onloadend = function(){
        this.opts.finished = true
        checkQueue()
      }

      xhr.opts.finished = false
      xhr.opts.callback = callback
      xhr.timeout = 1000*8
      xhr.open('get', url, true)
      xhr.send()
      
      function checkQueue(){
        var xhr = xhrQueue[0]
        while(xhr && xhr.opts.finished){
          if(xhr.status == 200 || xhr.status == 304){
            xhr.opts.callback(xhr.responseText)
          }
          xhrQueue.shift()
          xhr = xhrQueue[0]
        }

      }
    }

from frontend-interview.

LiuL0703 avatar LiuL0703 commented on July 21, 2024

这个问题是不是也可以用Promise 解决

from frontend-interview.

Inori-Lover avatar Inori-Lover commented on July 21, 2024

不是应该把第二个的填充放到第一个填充函数的callback?(所以也可以promise?

from frontend-interview.

ke1vin4real avatar ke1vin4real commented on July 21, 2024

核心是队列的**

from frontend-interview.

zzz945 avatar zzz945 commented on July 21, 2024

es6伪代码, 忽略细节和错误处理

let input = findInputElement()
let aPromise

function onButtonAClick () {
  aPromise = api.getA().then(text => input.value = text)
}

async function onButtonBClick () {
  await aPromise
  input.value = await api.getB()
}

from frontend-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.