學習 ASP.NET AJAX深入淺出系列課程(21):利用Microsoft AJAX Library開發用戶端組建(上)(Level 300)

來源:互聯網
上載者:User

今天學習21課了!……

直接貼代碼,根據我的理解寫的注釋,不保證全對,如果有哪裡說錯了,望各位仁兄提出,加以改正!謝謝

/// <reference name="MicrosoftAjax.js"/>
//註冊 namespace Demo
Type.registerNamespace("Demo");

Demo.Timer = function() {
///<summary>Timer組件</summary>
Demo.Timer.initializeBase(this);
this._interval = 1000;
this._timer = null;
}
Demo.Timer.prototype = {
///<summary>擷取_interval值</summary>
get_interval: function() {
return this._interval;
},
///<summary>設定_interval值</summary>
set_interval: function(value) {
if (this._interval != value) {
this._interval = value;
//debugger;
//如果注釋下面一句則不會觸發 propertyChanged 事件
this.raisePropertyChanged("interval");
if (this._timer) {
this.stop();
this.start();
}
}
},
///<summary>添加tick事件</summary>
add_tick: function(handler) {
this.get_events().addHandler("tick", handler);
},
///<summary>刪除tick事件</summary>
remove_tick: function(handler) {
this.get_events().removeHandler("tick", handler);
},
///<summary>回呼函數</summary>
_timerCallback: function() {
//擷取tick函數
var handler = this.get_events().getHandler("tick");
if (handler) {
handler(this, Sys.EventArgs.Empty);
}
},
///<summary>Timer開始</summary>
start: function() {
if (this._interval > 0) {
//createDelegate 這個老趙以前講過的,不是很理解
this._timer = window.setInterval(Function.createDelegate(this, this._timerCallback), this._interval);
}
},
///<summary>Timer停止</summary>
stop: function() {
if (this._timer) {
window.clearInterval(this._timer);
this._timer = null;
}
}
};
///<summary>註冊類</summary>
Demo.Timer.registerClass("Demo.Timer", Sys.Component);

以上指令碼儲存為 js/timer.js .

 

Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1_ajaxLibray.aspx.cs" Inherits="ajax_1_ajaxLibray" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AJAX Libray</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="sm">
<Scripts>
<asp:ScriptReference Path="~/js/timer.js" />
</Scripts>
</asp:ScriptManager>
<script type="text/javascript">
//頁面周期 [以下數字為執行順序,並不是按照寫js的順序來執行的]
/* Sys.Application.add_unload(function() { alert("Page_unload"); });//3
Sys.Application.add_load(function() { alert("Page_load"); }); //2
Sys.Application.add_disposing(function() { alert("page_disposing"); });//4
Sys.Application.initialize();
Sys.Application.add_init(function() { alert("Page_init"); }); // 1
*/
</script>
<div id="display">
</div>
Interval:
<input value="1000" id="txtInterval" />
<input type="button" value="change" onclick="ChangeInterval();" />
<span id="shwoInterval"></span>
<script type="text/javascript">
//頁面初始化化時
Sys.Application.add_init(function() {
//建立組件
/*
id: timer ,作為組件的屬性,以便後面使用 $find("timer") 找到該主鍵
tick : onTick , var handler = this.get_events().getHandler("tick"); 和這句有聯絡
調用start(),函數其實就是執行onTick()函數,注意看Timer js裡面的start()函數
*/
$create(Demo.Timer, { "id": "timer" }, { "tick": onTick, "propertyChanged": onPropertyChanged });
});
function ChangeInterval() {
var value = parseInt($get("txtInterval").value, 10);
//debugger; --調試時發現少寫了.value
$find("timer").set_interval(value);
/*
因為執行set_interval時裡面有執行
this.raisePropertyChanged("interval");
所以又會觸發onPropertyChanged()事件,
我試過如果注釋this.raisePropertyChanged("interval");
則不會觸發onPropertyChanged事件
*/

}
var count = 0;
function onTick() {
$get("display").innerHTML = ++count;
}
//這個函數 (怎麼就知道有2個參數呢?疑惑)
function onPropertyChanged(sender, args) {
//debugger;
var property = args.get_propertyName();
//debugger;
var value = sender["get_" + property].apply(sender);
$get("shwoInterval").innerHTML = property + "改變成了" + value;
}
//Load事件
function pageLoad() {
$find("timer").start();
}
</script>
</form>
</body>
</html>

明天繼續學習… ,堅持每天一個視頻! 還需要學習其他的呢?
最近還要考試sharepoint,全是英文!頭痛!
 
 

Technorati 標籤: ajax

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.