如何检查图像是否缓存在js中? [英] how to check if an image was cached in js?

查看:94
本文介绍了如何检查图像是否缓存在js中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我参加面试时,我被问到这个问题,我不知道如何回答。

when I joined an interview , I was asked the question, I didn't know how to answer it .

你知道这个问题的关键点吗?

do you know the key point of the question?

推荐答案

检查 完成属性>图像对象是 true

function is_cached(src) {
    var image = new Image();
    image.src = src;

    return image.complete;
}

它似乎有用(虽然它会加载图像,如果它不是' t在缓存中,可能不是你想要的):

It seems to work (although it'll load the image if it isn't in the cache, which might not be what you want):

> is_cached('http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3')
false
> is_cached('http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3')
true

这篇关于如何检查图像是否缓存在js中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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