Created
September 5, 2016 09:33
-
-
Save yoppi/06bb150341ddca4b08c6c320d59e4b5e to your computer and use it in GitHub Desktop.
同じsrcなんだけど実は裏で画像が更新されていて変化したタイミングで取ってきたいという贅沢な悩みを解決するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// LastModifiedの変化を検知してimgをロードする | |
let $ = require('jquery'); | |
module.exports = (imgSrc, callback) => { | |
let beforeLastModified; | |
let downloadImg = () => { | |
$.get(imgSrc, (data, status, xhr) => { | |
let lastModified = xhr.getResponseHeader('Last-Modified'); | |
if (!beforeLastModified || beforeLastModified === lastModified) { | |
beforeLastModified = lastModified; | |
setTimeout(downloadImg, 200); | |
} else if (beforeLastModified !== lastModified) { | |
callback(true); | |
} | |
}); | |
}; | |
downloadImg(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment