功能冷却

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

Function Cooldown

问题

以下是您的代码的翻译部分:

function onOpen(){
  var cooldown = 0;
  DevDisableCD();
  var menu = SpreadsheetApp.getUi().createMenu("r/place menu");
  menu.addSeparator();
  menu.addItem("设置颜色", "SetColor");
  menu.addItem("开发者 { 关闭冷却", "DevDisableCD");
  menu.addToUi();
};

function DevDisableCD(){
  cooldown <- cooldown - 1;
}

function DisableCD(){
  Utilities.sleep(300000);
  cooldown <- cooldown - 1;
}

function EnableCD(){
  cooldown <- cooldown + 1;
  DisableCD();
}

function SetColor() {
  if(cooldown = 0){
    var ui = SpreadsheetApp.getUi();
    var result = ui.prompt(
      '设置颜色',
      '请输入十六进制颜色代码(例如:#000000):',
      ui.ButtonSet.OK_CANCEL);
    var cellcolor = result.getResponseText();
    var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.getCurrentCell().setValue('a').activate();
    spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Place'), true);
    if(cellcolor != ''){
    spreadsheet.getActiveRangeList().setBackground(cellcolor);
    EnableCD();
    }
  } else{
    SpreadsheetApp.getActive().toast("您处于冷却中!", "每分钟只能填色一次");
  }
}

希望这对您有所帮助。如果您需要进一步的帮助,请随时告诉我。

英文:

I want to make it so someone can only run a function via a custom menu every 1 minute, when they run the function it will run a function and then make them wait 1 minute before letting them run it again. Here is my entire code:

function onOpen(){
  var cooldown = 0;
  DevDisableCD();
  var menu = SpreadsheetApp.getUi().createMenu("r/place menu");
  menu.addSeparator();
  menu.addItem("Set Color", "SetColor");
  menu.addItem("Dev { Disable CD", "DevDisableCD");
  menu.addToUi();
};
function DevDisableCD(){
  cooldown <- cooldown - 1;
}
function DisableCD(){
  Utilities.sleep(300000);
  cooldown <- cooldown - 1;
};
function EnableCD(){
  cooldown <- cooldown + 1;
  DisableCD();
};
function SetColor() {
  if(cooldown = 0){
    var ui = SpreadsheetApp.getUi();
    var result = ui.prompt(
      'Set Color',
      'Please enter your hex color (ie: #000000):',
      ui.ButtonSet.OK_CANCEL);
    var cellcolor = result.getResponseText();
    var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.getCurrentCell().setValue('a')
    .activate();
    spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Place'), true);
    if(cellcolor != ''){
    spreadsheet.getActiveRangeList().setBackground(cellcolor);
    EnableCD();
    };
  } else{
    SpreadsheetApp.getActive().toast("Your on cooldown!", "You can only color a pixel every minute");
  };
};

Edit: Yes I have tried Utilities.sleep()

答案1

得分: 2

这里是您要翻译的内容:

也许可以使用全局变量或一些状态管理器,您还可以使用lodash或Rxjs。
这是一个简单的全局变量解决方案:

let cooldown = false
const btn = document.getElementById("menu")

btn.addEventListener("click", () => {
console.log("inside func", cooldown)
setTimeout(() => {
cooldown = false
console.log("inside setTime", cooldown)
// 3秒
}, 3000)
if (!cooldown) {
// 其余的功能逻辑
console.log("pressed")
}

cooldown = true
})

您不在DOM中工作:

let cooldown = false;
const doSomething = () => {
console.log('inside func', cooldown);
setTimeout(() => {
cooldown = false;
console.log('inside setTime', cooldown);
// 3秒
}, 3000);
if (!cooldown) {
// 在这里做一些事情..其余的功能逻辑
consolelog('已执行');
}
cooldown = true;
};

英文:

Mybe use a global variable or some state managers, you can also use lodash or Rxjs.
here a simple global variable solution:

> let cooldown = false
> const btn = document.getElementById("menu")
>
> btn.addEventListener("click", () => {
> console.log("inside func", cooldown)
> setTimeout(() => {
> cooldown = false
> console.log("inside setTime", cooldown)
> // 3 seconds
> }, 3000)
> if (!cooldown ) {
> // rest of function logic
> console.log("pressed")
> }
>
> cooldown = true
> })

You aren't working in dom:

>

let cooldown = false;
const doSomething = () => {
  console.log('inside func', cooldown);
  setTimeout(() => {
    cooldown = false;
    console.log('inside setTime', cooldown);
    // 3 seconds
  }, 3000);
  if (!cooldown) {
    // do Something here..rest of function logic
    console.log('excuted');
  }
  cooldown = true;
};

huangapple
  • 本文由 发表于 2023年3月4日 02:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630654.html
匿名

发表评论

匿名网友

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

确定