js函数只执行一次

js函数只执行一次

function once(fn) {
    
    
	let executed = false;
	return function() {
    
    
		if(!executed) {
    
    
			executed = true;
			fn.apply(this.arguments);
		}
	};
}

const sayHelloOnce = once(function() {
    
    
	console.log('Hello');
});
sayHelloOnce(); // 输出: 'Hello'
sayHelloOnce(); // 不再输出任何内容

猜你喜欢

转载自blog.csdn.net/qq_41421033/article/details/130580809
今日推荐