顯示具有 Javascript 標籤的文章。 顯示所有文章
顯示具有 Javascript 標籤的文章。 顯示所有文章

2018年1月10日

[JS] 字串轉數字使用 *1比 parseInt快

先驗證 parseInt和 *1轉化後的結果是否相同:
var strNum = "10000"
var parseNum = parseInt(strNum)
var multiplyNum = strNum * 1
typeof strNum
// "string"
typeof parseNum
// "number"
typeof multiplyNum
// "number"
parseNum
// 10000
multiplyNum
// 10000

接著比較兩種方式的效能
function testA(time, str) {
    for (i=0;i<time;++i) {
        var tmp = parseInt(str);
    }
}
function testB(time, str) {
    for (i=0;i<time;++i) {
        var tmp = str * 1;
    }
}

console.time('testA');
testA(10000000, "12345678");
console.timeEnd('testA');
// testA: 1226.073974609375ms

console.time('testB');
testB(10000000, "12345678");
console.timeEnd('testB');
// testB: 622.14208984375ms

console.time('testA');
testA(10000000, "82736491");
console.timeEnd('testA');
// testA: 1202.213134765625ms

console.time('testB');
testB(10000000, "82736491");
console.timeEnd('testB');
// testB: 619.800048828125ms

console.time('testA');
testA(10000000, "1");
console.timeEnd('testA');
// testA: 95.97900390625ms

console.time('testB');
testB(10000000, "1");
console.timeEnd('testB');
// testB: 67.47216796875ms

console.time('testA');
testA(10000000, "");
console.timeEnd('testA');
// testA: 719.873779296875ms

console.time('testB');
testB(10000000, "");
console.timeEnd('testB');
// testB: 235.06103515625ms

console.time('testA');
testA(10000000, "0");
console.timeEnd('testA');
// testA: 92.878173828125ms

console.time('testB');
testB(10000000, "0");
console.timeEnd('testB');
// testB: 74.76416015625ms

2016年10月19日

金額 format string

var result = '0,0.' + Array(decimalPlace + 1).join('0')
result = result.replace(/\.\s*$/, "");
decimalPlace 0: "0,0"
decimalPlace 1: "0,0.0"
decimalPlace 2: "0,0.00"

參考:

Repeat Character N Times

2015年10月28日

javascript 一個元件用兩個 class註冊同一種事件的觸發順序

<input class="A B" />

$('.A').keyup(function () {
  ...
});

$(document).delegate('.B', 'keyup', function () {
  ...
});

B會比 A先執行

2012年9月14日

Ajax 後 網頁重新導向


async
類型:Boolean
默認值: true。
默認設置下,所有請求均為異步請求。如果需要發送同步請求,請將此選項設置為 false。
注意,同步請求將鎖住瀏覽器,用戶其它操作必須等待請求完成才可以執行。

$.ajax({
    url: '/usercontrols/ExportHighchartPhotoServices.ashx',
    type: 'POST',
    async: false,
    data: { ... },
    error: function (xhr, ajaxOptions, thrownError) {
        ...
    },
    success: function (response) {
        ...
    }
});

jQuery ajax - ajax() 方法

2012年8月17日

ASP.NET 在 behind-code註冊 javascript

BasePage.Current.ClientScript.RegisterStartupScript(</pre>
   this.GetType(),
   "resetActiveTab",
   "<script language=\"javascript\" type=\"text/javascript\">
        jQuery(document).ready(function(){;
            alert(\"xxx\")
        }
    );
    </script>"
this.Page.ClientScript.RegisterStartupScript(...);

reference:
 Go to specific tab after refresh

2012年6月25日

Javascript日常運用有感

最近終於感到平常工作才會用到的 Javascript、jQuery也有在非上班時間運用到的時候了!!

那就是 ~ 類似 Firefox上的 GreaseMonkey這個套件的功能
可以讓使用者在自己的瀏覽器上掛載 script語法,直接對自己瀏覽的頁面動手腳

這樣在逛論壇時,就可以自己寫 script把根本就不會去看的東西給隱藏起來,網頁頓時精簡許多!!


不過平常愛用 Chrome的人,可沒有 GreaseMonkey可用
還好 Chrome的擴充功能上有一款 Blank Canvas Script Handler,也有同樣的效果啦!!