简体   繁体   English

覆盖JavaScript和jQuery中的操作

[英]Override action in JavaScript and jQuery

In my requirement, a user can select multiple check-boxes. 根据我的要求,用户可以选择多个复选框。 However after checking each check-box I need to call the URL and pass selected checkbox value with time-delay of 3 seconds. 但是,在选中每个复选框后,我需要调用URL并以3秒的延迟传递选定的复选框值。 This delay is required, so that user can select/de-select another checkbox, old action should be canceled and new selected value(s) should be passed. 此延迟是必需的,以便用户可以选择/取消选择另一个复选框,应取消旧操作并传递新的选定值。

I know to call a function with delay, but don't know how to stop the last action with the new one. 我知道要延迟调用一个函数,但是不知道如何用新函数停止最后一个动作。

setTimeout(function(){ call_me(action) },3000);
function call_me(action)
{
    var url = action
    $(location).attr('href',url);
}

You have to use clearTimeout() , but first you need to assign the return of the setTimeout to a variable. 您必须使用clearTimeout() ,但是首先您需要将setTimeout的返回值分配给变量。

// declared outside of your code that sets the timeout.
var t;


// your code to be run when you want to set the timeout
clearTimeout(t);  // if there is no timeout already assigned to t, this line does nothing.
t = setTimeout(function(){ call_me(action) },3000);
function call_me(action)
{
    var url = action
    $(location).attr('href',url);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM