JavaScript synchronization options

前端 未结 6 877
庸人自扰
庸人自扰 2021-02-04 13:44

I was wondering whenever exists a solution to perform synchronization in JavaScript code. For example I have the following case: I\'m trying to cache some response values from A

6条回答
  •  我在风中等你
    2021-02-04 13:51

    I have found solution for my problem. I have to say it's not that perfect as I was looking for, but so far it works and I think about that more as temporarily work around.

    $.post( "url1", function( data)
    {
         // do some computation on data and then
         setSomeValue( data);
    });
    
    var setSomeValue = ( function()
    {
        var cache = {};
        return function( data)
        {
            if ( cache[data] == "updating")
            {
                 setTimeout( function(){ setSomeValue( data);}, 100);
                 return;
            }
            if ( !cache[date])
            {
                 cache[date] = updating;
                 $.post( "url2", function( another_data)
                 {
                      //make heavy computation on another_data
                      cache[data] = value;
                      // update the UI with value
                 });
            }
            else
            {
                 //update the UI using cached value
            }
        }
    })();
    

提交回复
热议问题