一个装饰器函数,用于减缓另一个 JavaScript 函数的执行。

huangapple go评论62阅读模式
英文:

a decorator function that slows down the execution of another function javascript

问题

// 一个装饰器函数,将任意函数的执行减慢 5 秒。

function someFunction(a, b) {
    console.log(a + b);
}

function slower(func, seconds) {
    // 装饰器代码
}

let slowedSomeFunction = slower(someFunction, 5);
slowedSomeFunction();
// 将在控制台输出 "You will get your result in 5 seconds"
// 5 秒后将显示 'someFunction' 的结果
// 尝试不成功,代码中存在错误

function someFunction(x) {
    alert(x);
}

function slower(func, seconds) {
    return function() {
        setTimeout(() => func.apply(this, arguments), seconds);
    }
}

let slowedSomeFunction = slower(alert, 5); 

slowedSomeFunction('You will get your result in 5 seconds');
英文:

a decorator function that slows down the execution of an arbitrary function by 5 seconds.

function someFunction(a, b) {
		console.log(a + b);
	}
function slower(func, seconds) {
// decorator code
}
let slowedSomeFunction = slower(someFunction, 5);
slowedSomeFunction()
// will output to the console "You will get you result in 5 seconds"
//...in 5 seconds will display the result of 'someFunction*'

attempts were unsuccessful, there are errors in the code

function someFunction(x) {
	alert(x);
}

function slower(func, seconds) {
	return function() {
		setTimeout(() => func.apply(this, arguments), seconds);
	}
}

let slowedSomeFunction = slower(alert, 5); 

slowedSomeFunction('You will get you result in 5 seconds');

答案1

得分: 0

我不太确定你试图做什么,但你的函数似乎可以工作。唯一的问题是 setTimeout() 接受的参数是毫秒,而不是秒,所以如果你想要秒数,你需要将其乘以 1,000:

function someFunction(x) {
    alert(x);
}

function slower(func, seconds) {
    return function() {
        setTimeout(() => func.apply(this, arguments), seconds*1000);
    }
}

let slowedSomeFunction = slower(someFunction, 5); 

slowedSomeFunction('冷静下来,你将在5秒内得到结果');
英文:

I'm not exactly sure to understand what you're trying to do but your function seems to work. The only issue is that the setTimeout() takes an argument in milliseconds and not seconds so you need to multiply it by 1 000 if you want seconds:

function someFunction(x) {
    alert(x);
}

function slower(func, seconds) {
    return function() {
        setTimeout(() => func.apply(this, arguments), seconds*1000);
    }
}

let slowedSomeFunction = slower(someFunction, 5); 

slowedSomeFunction('Chill out, you will get you result in 5 seconds');

答案2

得分: 0

尝试这个:

function someFunction(a, b) {
    console.log(a + b);
}

function slower(func, seconds) {
    return function() {
        console.log("你将在" + seconds + "秒内得到结果");
        setTimeout(() => func.apply(this, arguments), seconds * 1000);
    }
}

let slowedSomeFunction = slower(someFunction, 5);
slowedSomeFunction(2, 3);
英文:

Try this :

function someFunction(a, b) {
    console.log(a + b);
}

function slower(func, seconds) {
    return function() {
        console.log("You will get your result in " + seconds + " seconds");
        setTimeout(() => func.apply(this, arguments), seconds * 1000);
    }
}

let slowedSomeFunction = slower(someFunction, 5);
slowedSomeFunction(2, 3);

huangapple
  • 本文由 发表于 2023年2月14日 22:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定