Queue.js预加载图像会一直等待吗? (打回来) [英] Queue.js to preload images is waiting forever? (Callback)

查看:106
本文介绍了Queue.js预加载图像会一直等待吗? (打回来)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mbostock queue.js脚本通过执行类似操作来加载多个json文件:

I'm using mbostock queue.js script to load several json files, by doing something like that:

var q = queue()
.defer(d3.json, "world-110m.json")
.defer(d3.tsv, "world-country-names.tsv")
.await(ready);

就绪是在加载所有文件时要执行的功能.

where ready is the function to execute when everythin is loaded.

我想通过添加一个延迟来预加载图像.这可能吗?我已经尝试了几种方法,但是没有用.

I would like to preload an image by adding a defer. Is this possible? I have tried it several ways, but it doesn't work.

我想必须创建一个函数,但是我不能使它异步,并且队列永远等待着……

I suppose that a function must be created, but I can't make it asynchronous, and the queue keeps waiting forever...

推荐答案

这是queue.js对回调:

Here is what queue.js is expecting from the callbacks:

回调遵循Node.js约定,其中第一个参数是可选的错误对象,第二个参数是任务的结果.

The callbacks follow the Node.js convention where the first argument is an optional error object and the second argument is the result of the task.

因此,您的代码的简单版本可能如下所示:

Thus a simple version of your code might look like this:

var loadImage = function(src, cb) {
    var img = new Image();
    img.src = src;
    img.onload  = function(){ cb(null, img); };
    img.onerror = function(){ cb('IMAGE ERROR', null); };
};

queue()
    .defer(d3.json, "data/flare.json")
    .defer(d3.csv, "data/test.csv")
    .defer(loadImage, "img/test.png")
    .await( function(error, jsondata, csvdata, imagedata) {
        if (error) { console.log('error', error); } 
        else { console.log('success', jsondata, csvdata, imagedata) }
    });

我不确定要返回关于图像的什么(如果有的话),但是在上面的示例中,cb(null, img)我将返回整个对象.

I'm not sure what (if anything) you wanted to return about the image, but in the above example cb(null, img) I'm returning the whole object.

这篇关于Queue.js预加载图像会一直等待吗? (打回来)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆