Skip to content

Instantly share code, notes, and snippets.

View zhuochun's full-sized avatar

Zhuochun zhuochun

View GitHub Profile
@zhuochun
zhuochun / run_async.js
Created April 21, 2013 10:11
Run a large batch of async tasks separated
var complete = {}, completed = 0;
var i, thread = 23, total = 179, max = (total / thread | 0) + 1;
function startCount(idx, max) {
if (completed >= total) {
console.log("completed = " + completed);
console.log(complete);
return ;
} else if (idx === max || idx >= total) {
@zhuochun
zhuochun / fixnavbar.js
Created December 6, 2012 03:34
Scroll Fix on Top Navbar
// fix navbar on scroll
var $win = $(window)
, $nav = $('.navbar')
, navTop = $('.navbar').length && $('.navbar').offset().top - 10
, isFixed = 0
processScroll()
$win.on('scroll', processScroll)
@zhuochun
zhuochun / jQuery Plugin.js
Created October 28, 2012 15:47
jQuery Plugin Boilerplate
/* ========================================
* <Project> - <Description>
*
* Author:
* Last Edit:
* ========================================
* <License>
* ======================================== */
// the semi-colon before function invocation is a safety net against concatenated
@zhuochun
zhuochun / js template.js
Created October 28, 2012 15:46
RequireJS + jQuery
/* ========================================
* <Project> - <Description>
*
* Author:
* Last Edit:
* ========================================
* <License>
* ======================================== */
define(function(require, exports) {
@zhuochun
zhuochun / Underscore.js
Created October 22, 2012 09:04
Extend Methods in JavaScript
/*
-> http://underscorejs.org/#extend
extend(destination, *sources)
Copy all of the properties in the source objects over to the destination object, and return the destination object.
It's in-order, so the last source will override properties of the same name in previous arguments.
*/
@zhuochun
zhuochun / Backbone.js
Created October 22, 2012 08:07
Define JavaScript Frameworks
(function(){
// Initial Setup
// -------------
// Save a reference to the global object (`window` in the browser, `exports`
// on the server).
var root = this;
// Save the previous value of the `Backbone` variable, so that it can be
@zhuochun
zhuochun / Convert to Number.js
Last active October 11, 2015 22:28
JavaScript Snippets from Everywhere
+1 === 1;
+0 === 0;
+undefined == NaN;
+null === 0;
+({1:2,2:3}).length == NaN
// Underscore -> https://github.com/documentcloud/underscore/blob/master/underscore.js#L80
@zhuochun
zhuochun / pubsub.md
Created July 23, 2012 07:44 — forked from addyosmani/pubsub.md
Four ways to do Pub/Sub with jQuery 1.7 and jQuery UI (in the future)

#Four Ways To Do Pub/Sub With jQuery 1.7 and jQuery UI (in the future)

Between jQuery 1.7 and some of work going into future versions of jQuery UI, there are a ton of hot new ways for you to get your publish/subscribe on. Here are just four of them, three of which are new.

(PS: If you're unfamiliar with pub/sub, read the guide to it that Julian Aubourg and I wrote here http://msdn.microsoft.com/en-us/scriptjunkie/hh201955.aspx)

##Option 1: Using jQuery 1.7's $.Callbacks() feature:

$.Callbacks are a multi-purpose callbacks list object which can be used as a base layer to build new functionality including simple publish/subscribe systems. We haven't yet released the API documentation for this feature just yet, but for more information on it (including lots of examples), see my post on $.Callbacks() here:

@zhuochun
zhuochun / core.test.js
Created July 12, 2012 14:13 — forked from drewwells/core.test.js
RequireJS and QUnit sitting in a tree
//Wait for relevant code bits to load before starting any tests
define(['core.js'], function( core ) {
module("Core Tests");
test("Test core methods", function(){
expect(2);
equals( 1, 1, "A trivial test");
ok( true, "Another trivial test");
});
@zhuochun
zhuochun / Singleton pattern
Created June 15, 2012 04:34
Singleton JavaScript Pattern
// http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html
var Singleton = (function() {
var instance = null;
function PrivateConstructor() {
var rand = Math.round(Math.random() * 100);
this.getRand = function() {
return rand;
}