Home > Web Front-end > JS Tutorial > Use apply to make the javascript function execute only once code_javascript skills

Use apply to make the javascript function execute only once code_javascript skills

WBOY
Release: 2016-05-16 18:24:17
Original
1047 people have browsed it
Copy code The code is as follows:

var obj = new Object();
obj.triggerOnce = function(fn) { //Control the function to trigger only once
return function() {
try {
fn.apply(this, arguments);
}
catch (e) {
var txt = "There was an error on this page.nn";
txt = "Error message: " e.message "nn";
txt = "Error name: " e.name "nn ";
//alert(txt);//This line may need to be commented out on the official platform
}
finally {
fn = null;
}
}
}

Call form:
Copy code The code is as follows:

function showMsg(arg){
alert(arg);
}
var fn=obj.triggerOnce(showMsg);
fn(1);//Execute only once
fn( 2);//fn is not called

Finally, regarding the apply method of javascript, you can simply refer to this old article.
【External article】Use javascript to obtain the value of querystring
In common websites, we often encounter URLs in the form of "XXXX.aspx?username=jeffwong&address=Beijing", through the Request of asp.net It is easy to get the key value of the object, and it is not difficult to use js. You can use the following function:
Code
Copy code The code is as follows:

var urlStrs = location.search; //QueryString
var Request = {};
Request.Count = 0;
Request.Add = function (name, value) {
if (Request.hasOwnProperty(name) == false) {
Request.Count; //If it does not exist, add 1 to the count
}
Request[name] = value;
return true;
}
Request.QueryString = function(name) {//Get the corresponding key value
var key = name.toString().toLocaleLowerCase(); //No Case sensitive
var result = null;
try {
// if (Request.hasOwnProperty(key)) {
result = Request[key];
// }
}
catch (e) {
//alert(e.message);
}
return result;
}
Request.Init = function(urlQueryString) {
if (urlQueryString.indexOf("?") != -1) {
var url = urlQueryString.substr(1)
var strArr = url.split("&");
for (var i = 0; i < strArr.length; i ) {
var strChildArr = strArr[i].split("=");
var name = String(strChildArr[0]).toLowerCase();
var value = unescape(strChildArr[1]); //unescape decoding
this.Add(name, value); //Add
}
}
}

The test function is as follows:
Code
Copy code The code is as follows:

/ /Test function
function test() {
Request.Init(urlStrs); //Initialize request
alert(Request.Count);
var name = "username";
var value = Request.QueryString(name);
alert(Request[name]);
alert(Request.QueryString(name));
name = "address"
alert(Request[name]) ;
alert(Request.QueryString(name));
name = "age";
alert(Request[name]);
alert(Request.QueryString(name));
}

It should be noted that the parameters in the URL are usually encoded. This article uses the more commonly used unescape for decoding. In fact, regarding the URL parameter encoding, there are usually escape, encodeURIComponent and encodeURI. Of these three encoding functions, escape is a built-in function of the window object, while the other two are built-in functions of JavaScript. Their decoding functions also have a one-to-one correspondence (you can refer to this article). The corresponding inversion functions are respectively are unescape, decodeURIComponent and decodeURI.
Author: Jeff Wong
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template