在此之后,我该如何执行操作(一小部分)在jQuery javaScript中传递



我必须在延迟时间后执行操作(一秒钟)。

实际上我有此代码部分:

$("thead.opening").click(function () {
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    alert("INTO second function, chrome: " + is_chrome);
    $(this).next().css('width', '10000000em');
    $(this).next().css('display', 'table-row-group');
});

我需要做的是替换此警报():

alert("INTO second function, chrome: " + is_chrome);

用一段时间的操作。

我该怎么做?

thx

您可以使用纯JavaScript函数settimeout

setTimeout(
  function() 
  {
    //Execute the code to be run
  }, 1000);

最终解决方案

$("thead.opening").click(function () {
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

    setTimeout(
      function() 
      {
        //Execute the code to be run
    $(this).next().css('width', '10000000em');
    $(this).next().css('display', 'table-row-group');
      }, 1000);

});

使用超时;在JavaScript中,您可以使用settimeout到:

在指定延迟后调用函数或执行代码段。

喜欢:

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var $myNext=$(this).next();
if (is_chrome) {
    window.setTimeout(function() {
         $myNext.css({width: '10000000em', display: 'table-row-group'});
    }, 1000);
}

演示:http://jsfiddle.net/4pxedhzd/

ref:https://developer.mozilla.org/en-us/docs/web/api/windowtimers/settimers/settimeout

,而不是在代码中间有典型的"等待",我建议使用回调,因为这是JavaScript正常使用的。

我建议使用" settimeout"函数使javaScript调用等待一些特定的时间,然后再做下一件事情。以下是您的场景中可以使用的:

$("thead.opening").click(function () {
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    setTimout(waitedCall(), millisecondsToWait);
});
//This function will now be called after "millisecondsToWait" ms. And then execute the rest of the code
function waitedCall()
{
    $(this).next().css('width', '10000000em');
    $(this).next().css('display', 'table-row-group');
}

替换

alert("INTO second function, chrome: " + is_chrome);

if(is_chrome) {
    var that = this;
    window.setTimeout(function() {
        $(that).next().css({'width':'10000000em', 'display':'table-row-group'});
    }, 1000);
}

这将在1000毫秒后运行该功能(= 1秒)。您必须设置that = this,因为this在功能中具有另一个上下文。

jsfiddle演示:https://jsfiddle.net/0eajx8sv/

最新更新