Skip to content

Instantly share code, notes, and snippets.

View gmailzj's full-sized avatar

lake gmailzj

View GitHub Profile
@gmailzj
gmailzj / timeoutify.js
Last active April 4, 2018 09:54
timeoutify.js
// 超时调用问题 只是思路
function timeoutify(fn,delay) {
var intv = setTimeout( function(){
intv = null;
fn( new Error( "Timeout!" ) );
}, delay )
;
return function() {
// timeout hasn't happened yet?

移动端H5图片压缩上传

大体的思路是,部分API的兼容性请参照caniuse

  1. 利用FileReader,读取blob对象,或者是file对象,将图片转化为data uri的形式。
  2. 使用canvas,在页面上新建一个画布,利用canvas提供的API,将图片画入这个画布当中。
  3. 利用canvas.toDataURL(),进行图片的压缩,得到图片的data uri的值
  4. 上传文件。

步骤1当中,在进行图片压缩前,还是对图片大小做了判断的,如果图片大小大于200KB时,是直接进行图片上传,不进行图片的压缩,如果图片的大小是大于200KB,则是先进行图片的压缩再上传:

@gmailzj
gmailzj / gist:f6e2ccaa444f113656c7d27b69668ba4
Created February 15, 2017 03:37 — forked from dberesford/gist:28876b39d26b02b7683a
Sample node.js leak detection using memwatch and heapdump
var http = require('http');
var util = require('util');
var heapdump = require('heapdump');
var memwatch = require('memwatch');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
for (var i=0; i<1000; i++) {
server.on('request', function leakyfunc() {
@gmailzj
gmailzj / speedTest.js
Created December 21, 2016 07:58 — forked from fiture/speedTest.js
一个测速的JS
var isMobile = !!navigator.userAgent.match(/AppleWebKit.*Mobile.*/);// || !!navigator.userAgent.match(/AppleWebKit/);
function log(s) {
try {
window.console && console.log(s);
} catch (ignore) { }
}
@gmailzj
gmailzj / webkitmediasource-is-type-supported.html
Created December 3, 2016 03:11 — forked from granoeste/webkitmediasource-is-type-supported.html
[JavaScript][HTML5][MediaSource] MediaSource.isTypeSupported
<!DOCTYPE html>
<html>
<head>
<script>
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
function testTypes(types) {
for (var i = 0; i < types.length; ++i)
console.log("MediaSource.isTypeSupported(" + types[i] + ") : " + MediaSource.isTypeSupported(types[i]));
}
@gmailzj
gmailzj / HEY-YOU.md
Created April 14, 2016 09:34 — forked from cowboy/HEY-YOU.md
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
@gmailzj
gmailzj / deepClone.js
Last active March 4, 2016 03:58
deepClone-对象深度复制
/**
* Note: This algorithm actually implements only RegExp, Array, and Date special objects.
* You can implement other special cases depending on your needs.
*/
function clone(objectToBeCloned) {
// Basis.
if (!(objectToBeCloned instanceof Object)) {
return objectToBeCloned;
}