setImmediate之前的setTimeout

时间:2016-02-18 14:34:16

标签: javascript asynchronous timeout settimeout

此代码打印setTimeout在setImmediate之前。为什么会这样?我以为所有setImmediates都在setTimeouts之前运行。

setImmediate(function() {
    console.log("i");
});

setTimeout(function() {
    console.log("t");
}, 0);

var i;
for (i = 0 ; i < 3000000000 ; ++i) {}

1 个答案:

答案 0 :(得分:0)

根据NodeJS Event Loop docs

  

如果两者都是从主模块中调用的,则时序将受进程性能的限制(这可能会受到计算机上运行的其他应用程序的影响)。

但是实际上确实会发生这种情况:如果在一个I / O周期内移动两个调用,则总是首先执行立即回调:

public void RequestRewardAd()
{
    AdRequest request = new AdRequest.Builder().Build();

    rewardBasedVideo.LoadAd(request, rewardBasedVideoId);
}
public void ShowRewardAd()
{
    if (rewardBasedVideo.IsLoaded())
    {
        rewardBasedVideo.Show();
    }
}

public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
    Debug.Log("HandleRewardBasedVideoLoaded event received");
}

public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
    Debug.Log("HandleRewardBasedVideoFailedToLoad event received with message: "+ args.Message);
}

public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
    Debug.Log("HandleRewardBasedVideoOpened event received");
}

public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
    Debug.Log("HandleRewardBasedVideoStarted event received");
}

public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
    Debug.Log("HandleRewardBasedVideoClosed event received");
    RequestRewardAd();
}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    string type = args.Type;
    PlayerPrefs.SetFloat("Revive", (int)args.Amount);
}

public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
    Debug.Log("HandleRewardBasedVideoLeftApplication event received");
}