This file contains hidden or 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
| // 参考:https://stackoverflow.com/questions/6937863/json-stringify-so-that-arrays-are-on-one-line | |
| // 元素皆为原始类型的数组不换行 | |
| // 举例说明:元素皆为原始类型的数组格式化为 | |
| // a: [1, 2, 3] | |
| // 而不是 | |
| // a: [ | |
| // 1, | |
| // 2, | |
| // 3 | |
| // ] |
This file contains hidden or 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
| // 链接:https://www.zhihu.com/question/31636244/answer/52835780 | |
| // 来源:知乎 | |
| async function test() { | |
| console.log('Hello') | |
| await sleep(1000) | |
| console.log('world!') | |
| } | |
| function sleep(ms) { |
This file contains hidden or 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
| // Reference:https://github.com/jkchao/blog/issues/8 | |
| function getType (val) { | |
| const str = Object.prototype.toString.call(val) | |
| return /^\[object (.*)\]$/.exec(str)[1] | |
| } |
This file contains hidden or 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
| class MyComp extends Component { | |
| // default state | |
| state = { fetching: 0 } | |
| componentWillMount() { | |
| // Setting state here won't trigger re-render | |
| this.setState({ fetching: 1 }); | |
| } |
This file contains hidden or 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: setParam('...?a=2&b=4', 'a', 3); | |
| function setParam(uri, key, val) { | |
| return uri | |
| .replace(new RegExp("([?&]"+key+"(?=[=&#]|$)[^#&]*|(?=#|$))"), "&"+key+"="+encodeURIComponent(val)) | |
| .replace(/^([^?&]+)&/, "$1?"); | |
| } |
This file contains hidden or 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
| /** | |
| * numberWithCommas 数字千分位逗号处理 | |
| * @param {Number} x - 需要处理的数字 | |
| * @return {String} - 处理后的数字 | |
| */ | |
| export default function numberWithCommas(x) { | |
| if (!x) { | |
| return '--'; | |
| } | |
| let parts = x.toString().split("."); |
This file contains hidden or 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
| /* | |
| Lazy Loading Images | |
| https://css-tricks.com/snippets/javascript/lazy-loading-images/ | |
| */ | |
| function loadImage (el, fn) { | |
| var img = new Image() | |
| , src = el.getAttribute('data-src'); | |
| img.onload = function() { | |
| if (!! el.parent) |
This file contains hidden or 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
| // stackoverflow: https://stackoverflow.com/a/2627493/6339408 | |
| var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds | |
| var firstDate = new Date(2008,01,12); | |
| var secondDate = new Date(2008,01,22); | |
| var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))); // -> 10 |
This file contains hidden or 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
| // seconds to hh:mm:ss | |
| // use: alert("5678".toHHMMSS()); | |
| String.prototype.toHHMMSS = function () { | |
| var sec_num = parseInt(this, 10); // don't forget the second param | |
| var hours = Math.floor(sec_num / 3600); | |
| var minutes = Math.floor((sec_num - (hours * 3600)) / 60); | |
| var seconds = sec_num - (hours * 3600) - (minutes * 60); | |
| if (hours < 10) {hours = "0"+hours;} | |
| if (minutes < 10) {minutes = "0"+minutes;} | |
| if (seconds < 10) {seconds = "0"+seconds;} |
This file contains hidden or 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
| $(function(){ | |
| var minHeight = document.body.offsetHeight; | |
| document.getElementById("main").style.minHeight = minHeight+'px'; | |
| document.getElementById("sidebar").style.minHeight = minHeight+'px'; | |
| }) |
NewerOlder