JavaScript Learning notes Black.caffeine 09.11.28_ Basics

Source: Internet
Author: User
Tags setinterval
1. Previously written is always a decentralized function, what function to write what function, feel not tidy, so this time to write the package of classes, used to be good, but the transfer parameters encountered a lot of problems, so, a lot of information, summarized as follows:
1 Dynamic binding Event issues:
You need to bind the OnClick event to an object, such as a list item. Need to use AddEventListener or attachevent for bar function operations to add to the event instead of overwriting, but Attachevent does not support FF,FF only with AddEventListener. So, it takes a function to synthesize them both, so this function is born:
Copy Code code as follows:

function addEventHandler (Otarget, Seventtype, Fnhandler)
{
if (Otarget.addeventlistener)
{Otarget.addeventlistener (Seventtype, Fnhandler, false);}
else if (otarget.attachevent)
{otarget.attachevent (' on ' + Seventtype, Fnhandler);}
else{otarget[' on ' + seventtype] = Fnhandler;}
}

2 Pass This parameter problem:
Because of me, functions and attributes are encapsulated within a class, so events such as binding onclick have a problem, such as addEventHandler (This.elems[i], "click", this. Move), which makes an error, because when the OnClick event occurs, the invocation of this does not point to the encapsulated class, and the application () is used to ~--a method of applying an object, replacing the current object with another object. Specific format I don't have to say, a lot of online ~ function:
Copy Code code as follows:

var Bind = function (Object,func) {
var args = Array.prototype.slice.call (arguments). Slice (2);
return function () {
Return func.apply (Object,args);
}
}

Call:
This._fnmove=bind (this,this.move,i);//this.move is a member function that I define, encapsulated in a class
this.elems[i].onclick=this._fnmove;//It is also possible to replace the words above, except that the onclick event is replaced by This._fnmove instead of adding this._fnmove in
addEventHandler (This.elems[i], "click", This._fnmove);
So it's OK ~
Ps.call () is basically the same function, but the specific parameters are different
2.setInterval problem
1) and the difference between the settimeout
In general, SetTimeout executes only once, (of course, if you repeatedly call settimeout in a function, you can do it repeatedly) and setinterval can be repeated until clearintercal ()
2 incompatible problems under IE
This problem tortured me 50% of the time, oh buy karma, later is not to spend half of their life in the war with IE ...
Originally, in the Chrome,ff,safari all run very well, I am quite excited, so that I forgot ie ... Later on IE on a try, the result, finished, modified, Google (here for the verb, hehe), basically spent half a day, and finally finished. Before, the statement is like this: This.timer=setinterval (this.unfold,5,this.divs[index],this), the result is not very well under IE. Finally, in a hero's article, see the following description: in IE, settimeout and setinterval is not supported by parameter transfer. The problem was solved quickly, and I was too vegetable ~
The problem-solving function is as follows:
Copy Code code as follows:

var mysetinterval = setinterval;
Window.setinterval = function (callback, interval)
{
var args = Array.prototype.slice.call (arguments, 2);
function Callfn () {callback.apply (null, args);}
Return Mysetinterval (CALLFN, interval);
}

var mysettimeout = settimeout; Modify SetInterval
Window.settimeout = function (callback, timeout)
{
var args = Array.prototype.slice.call (arguments, 2);
function Callfn () {callback.apply (null, args);}

Then use Window.settimeout or window.setinterval to call it
My statement is modified as follows:
This.timer=window.setinterval (This.unfold,5,this.divs[index],this); Where the this.divs[index],this is passed two parameters
Thank the Warrior again, though he does not know me ~
At present, in IE still a little bit of typesetting problem, continue to learn ~ Full rabbit!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.