nodejs中传参判断 - CNode技术社区

nodejs中传参判断
发布于 5 年前 作者 areny7 3651 次浏览 来自 问答

QQ截图20210316193230.png

就像图片中的代码片段,我该如何去编写这样的代码,能否实现?(这段代码有问题,但是应该可以表达我要的东西) 在触发某个条件的时候让value的值为true,从而跳出这个死循环,但是不希望这个value的值是在函数外面。

8 回复

setTimeout(fn, 1000, value),你是想要这样?

@linliny 不是的,这个函数是个死循环,我要的结果就是能控制住这个死循环什么时候能停止,那么就只能判断value的值了,但是判断写在函数外面的话,很乱,就想看看当作形参来判断。

function func(value) {
	if (value === true) return;
	const random = Math.round(Math.random() * 10);
	value = random === 5;
	console.log(value);
	console.log(random);
	setTimeout(func, 1000, value);
}
func(false);

这种吗

let flag = true; function fn(v) { if (v == flag) { console.log(‘stop’) return } console.log(v) setTimeout(fn.bind(null, v), 1000); } fn(false) setTimeout(() => { flag = false }, 1000 * 10);

不知是不是这个意思

// 这种?
setTimeout(fn, 1000, value);
// 这种?
if (global.value) return;
// 这种?
const timeout = setTimeout(fn, 1000);
if (value) clearTimeout(timeout);

@bubao 能不能就是func(false)作为启动死循环,然后如果停止的话,就执行func(true);

@areny7 我写了两个例子

发布订阅模式

class Subject {
 constructor() {
 this.state = 0
 this.observers = []
		this.setState = this.setState.bind(this)
 }
 getState() {
 console.log(this.state)
 return this.state
 }
 setState(state) {
 this.state = state
 this.notifyAllObservers()
 }
	detach(observer) {
 const observerIndex = this.observers.indexOf(observer);
 if (observerIndex === -1) {
 return;
 }
 this.observers.splice(observerIndex, 1);
 }
 notifyAllObservers(){
 this.observers.forEach(observer => {
 observer.update()
 })
 }
 attach(observer) {
 this.observers.push(observer)
 }
}
class Observer {
	/**
	 *Creates an instance of Observer.
	 * @author bubao
	 * @date 2021年03月21日
	 * @param {String} name
	 * @param {Subject} subject
	 * @memberof Observer
	 */
	constructor(name, subject) {
 this.name = name
 this.subject = subject
 this.subject.attach(this)
 }
 update(){
		if (!this.subject.state) {
			this.subject.detach(tick)
			return;
		}
		console.log("update",this.subject.state)
		setTimeout(this.subject.setState, 1000, this.subject.state);
 }
}
const subject = new Subject()
const tick = new Observer('tick',subject)
subject.setState(true)
setTimeout(()=>{
	subject.setState(false)
},5000)

另一个就low一点了,但是方便理解

class Test {
	constructor(){
		this.count = 0;
		this.func = this.func.bind(this)
		// this.func(true);
	}
	setState(value){
		this.value = value;
		this.value && this.func();
	}
	func() {
		if (this.value === false) return;
		this.count += 1;
		console.log(this.count,this.value);
		setTimeout(this.func, 1000);
	}
}
const test = new Test();
test.setState(true)
setTimeout(()=>{
	test.setState(false)
},5000)
setTimeout(()=>{
	test.setState(true)
},10000)
setTimeout(()=>{
	test.setState(false)
},15000)

也可以把func写在外边,那可以改成这样

class Test {
	constructor(cb){
		this.state = true
		this.funcName = cb.name
		this[this.funcName] = cb.bind(this)
	}
	setState(state){
		this.state = state;
		this.state && this[this.funcName]();
	}
}
let count = 0
const test = new Test(function func() {
	if (this.state === false) return;
	count += 1;
	console.log(count,this.state);
	// this.func 要与callback函数名一样
	setTimeout(this.func, 1000);
});
test.setState(true)
setTimeout(()=>{
	test.setState(false)
},5000)
setTimeout(()=>{
	test.setState(true)
},10000)
setTimeout(()=>{
	test.setState(false)
},15000)

这样写就需要在new的时候传一个非匿名函数。不传函数会报错。这样就把方法分离出来了,但是也有坏处,就是在vscode中,在callback函数里this显示是any。

用闭包

function fun() {
 let interval;
 const log = ()=>{
 console.log('console');
 }
 return (value)=>{
 if(value === true){
 interval = setInterval(log, 1000);
 } else {
 clearInterval(interval)
 }
 }
}
const test = fun();
test(true);
setTimeout(() => {
 test(false);
}, 5000);
回到顶部

AltStyle によって変換されたページ (->オリジナル) /