OAF 使用 javascript 使某个按钮在5秒内不能重复点击

首先要保证按钮是BUTTON,并且按钮事件设置firePartialAction。

 

public class CuxXXXXPGCO
  extends OAControllerImpl
{
  public static final String RCS_ID = "$Header$";
  public static final boolean RCS_ID_RECORDED = 
    VersionInfo.recordClassVersion(RCS_ID, "%packagename%");


private String disableButtonMultiClickByIdJs ="function submitApprove(evt) { \n" +
" console.log('function start evt ' + evt);\n" +
" disabledButton(); \n" +
" console.log('evt execute');\n" +
" MyPeriodicalExecuter(); \n" +
" } \n" +
" function disabledButton(){ \n" +
" var text = document.getElementById(\"Test\");\n" + //此处的ID改为你要在5秒后启用的ID
" console.log('Test');\n" +
" text.disabled=true;\n" +
" } \n" +
" \n" +
" function MyPeriodicalExecuter(){ \n" +
" succ.loop=0; \n" +
" console.log('111');\n" +
" sh=setInterval(succ,1000); \n" +
" console.log('777');\n" +
" } \n" +
" \n" +
" function succ(){ \n" +
" console.log('222');\n" +
" with(arguments.callee){ \n" +
" console.log('333');\n" +
" console.log('loop = '+loop);\n" +
" loop++;\n" +
" //obj.value=str+\"(\"+(loop++)+\"/15)retry\"; \n" +
" if (loop > 5 ){ \n" +
" console.log('444');\n" +
" enabledButton(); \n" +
" console.log('555');\n" +
" clearInterval(sh); \n" +
" console.log('666');\n" +
" return; \n" +
" } \n" +
" } \n" +
" } \n" +
" \n" +
" function enabledButton(){ \n" +
" console.log('enabledButton evt End');\n" +
" var text = document.getElementById(\"Test\");\n" + //此处的ID改为你要在5秒后启用的ID
" console.log('text getElementById FinSign');\n" +
" text.disabled=false; \n" +
" }";

/**
   * Layout and page setup logic for a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processRequest(OAPageContext pageContext,
                             OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
    
      pageContext.removeJavaScriptFunction("submitApprove");
//      pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickJs);
      pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickByIdJs);
      String submitJs = "javascript:submitApprove(this.id);";


      OAButtonBean button = (OAButtonBean)webBean.findChildRecursive("Test");

      button.setAttributeValue(OAWebBeanConstants.ON_CLICK_ATTR,submitJs);  //两个方法是一致的
//      button.setOnClick(submitJs);  //两个方法是一致的

    //your other business logic ...
  }

 

/**
* Procedure to handle form submissions for form elements in
* a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processFormRequest(OAPageContext pageContext,
OAWebBean webBean)
  {

  //your business logic 

}
}

此方法仍然有一个可以优化的地方,js代码中getElementById中的id值是写死了的,可以其实通过传入button.id的形式,使用全局变量,使得查找button通过id的方式,代码如下

1. 要清楚在哪个地方声明定义的是 全局变量 还是 局部变量 。
2. 全局变量所带来的 bug 问题非常多,可能很偶然的把代码放到其他页面里,把新页面的同名变量给覆盖,造成隐藏的 bug 。最好尽量少用全局变量。

所以这里要使用全局变量还是局部变量就见仁见智了

(不过经过测试,在IE浏览器中,此段代码不能正常运行,导致Test Button中的业务逻辑不能正常执行,猜测可能是Console.log引起的,建议删除Console.log,不然IE浏览器不能正确运行。)

(IE不支持console.log,会阻止程序的进一步执行)

public class CuxXXXXPGCO
extends OAControllerImpl
{
public static final String RCS_ID = "$Header$";
public static final boolean RCS_ID_RECORDED =
VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

//禁用按钮在5秒内连续点击
private String disableButtonMultiClickByIdJs ="var buttonId ='buttonId';\n" + //JavaScript全局变量

//全局变量所带来的 bug 问题非常多,可能很偶然的把代码放到其他页面里,把新页面的同名变量给覆盖,造成隐藏的 bug 。最好尽量少用全局变量。
"function submitApprove(evt) { \n" +
" buttonId = evt;\n" +
" disabledButton(buttonId); \n" +
" MyPeriodicalExecuter(); \n" +
" } \n" +
" function disabledButton(){ \n" +
" var text = document.getElementById(buttonId);\n" + //此处的ID改为你要在5秒后启用的ID
" text.disabled=true;\n" +
" } \n" +
" \n" +
" function MyPeriodicalExecuter(){ \n" +
" succ.loop=0; \n" +
" sh=setInterval(succ,1000); \n" +
" } \n" +
" \n" +
" function succ(){ \n" +
" with(arguments.callee){ \n" +
" loop++;\n" +
" if (loop > 5 ){ \n" +
" enabledButton(); \n" +
" clearInterval(sh); \n" +
" return; \n" +
" } \n" +
" } \n" +
" } \n" +
" \n" +
" function enabledButton(){ \n" +
" var text = document.getElementById(buttonId);\n" + //此处的ID改为你要在5秒后启用的ID
" text.disabled=false; \n" +
" return; \n" +
" }";

 

/**
* Layout and page setup logic for a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processRequest(OAPageContext pageContext,
OAWebBean webBean)
{
super.processRequest(pageContext, webBean);

pageContext.removeJavaScriptFunction("submitApprove");
// pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickJs);
pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickByIdJs);
String submitJs = "javascript:submitApprove(this.id);";

 

OAButtonBean button = (OAButtonBean)webBean.findChildRecursive("Test");

button.setAttributeValue(OAWebBeanConstants.ON_CLICK_ATTR,submitJs); //与下面的等价
// button.setOnClick(submitJs);//与上面的等价

 

//

}

 

 

/**
* Procedure to handle form submissions for form elements in
* a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processFormRequest(OAPageContext pageContext,
OAWebBean webBean)
{

//your logic

}

}

 

 

同时还有一个未解决的bug,如果在按钮执行之后发生了跳转,通过控制台可以看到loop会一直递增,直到发生下一个事件时终止。

 

 

Chrome浏览器在开启了F12日志诊断功能之后,可以看到console.log输出的日志如下

转载于:https://www.cnblogs.com/huanghongbo/p/6098314.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值