te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Wait until div is not visible to process next line - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Wait until div is not visible to process next line - Stack Overflow

programmeradmin3浏览0评论

I need to write some code which is supposed to wait until a predefined div is no longer visible in order to process the next line. I plan on using jQuery( ":visible" ) for this, and was thinking I could have some type of while loop. Does anyone have a good suggestion on how to acplish this task?

$( document ).ready(function() {
    $(".scroller-right" ).mouseup(function( event ) {
        alert('right');
        pollVisibility();
    });
});

function pollVisibility() {
     if ($(".mstrWaitBox").attr("visibility")!== 'undefined') || $(".mstrWaitBox").attr("visibility") !== false) { 
            alert('inside else');
            microstrategy.getViewerBone()mands.exec('refresh');       
     } else {
              setTimeout(pollVisibility, 100);
     }
}

$( document ).ready(function() {
    $(".scroller-right" ).mouseup(function( event ) {
        alert('right');
        pollVisibility();
    });
});

function pollVisibility() {
     if (!$(".mstrWaitBox").is(":visible")) {
        alert('inside if');
        microstrategy.getViewerBone()mands.exec('refresh');     
    } else {
        setTimeout(pollVisibility, 100);
    }
}

div when not visible:

<div class=​"mstrWaitBox" id=​"divWaitBox" scriptclass=​"mstrDialogImpl" dg=​"1" ty=​"edt">​
</div>​

div when visible:

<div class=​"mstrWaitBox" id=​"divWaitBox" scriptclass=​"mstrDialogImpl" dg=​"1" ty=​"edt" visibility="visible">​
</div>​

I need to write some code which is supposed to wait until a predefined div is no longer visible in order to process the next line. I plan on using jQuery( ":visible" ) for this, and was thinking I could have some type of while loop. Does anyone have a good suggestion on how to acplish this task?

$( document ).ready(function() {
    $(".scroller-right" ).mouseup(function( event ) {
        alert('right');
        pollVisibility();
    });
});

function pollVisibility() {
     if ($(".mstrWaitBox").attr("visibility")!== 'undefined') || $(".mstrWaitBox").attr("visibility") !== false) { 
            alert('inside else');
            microstrategy.getViewerBone().mands.exec('refresh');       
     } else {
              setTimeout(pollVisibility, 100);
     }
}

$( document ).ready(function() {
    $(".scroller-right" ).mouseup(function( event ) {
        alert('right');
        pollVisibility();
    });
});

function pollVisibility() {
     if (!$(".mstrWaitBox").is(":visible")) {
        alert('inside if');
        microstrategy.getViewerBone().mands.exec('refresh');     
    } else {
        setTimeout(pollVisibility, 100);
    }
}

div when not visible:

<div class=​"mstrWaitBox" id=​"divWaitBox" scriptclass=​"mstrDialogImpl" dg=​"1" ty=​"edt">​
</div>​

div when visible:

<div class=​"mstrWaitBox" id=​"divWaitBox" scriptclass=​"mstrDialogImpl" dg=​"1" ty=​"edt" visibility="visible">​
</div>​
Share Improve this question edited Oct 6, 2016 at 11:15 user247702 24.2k18 gold badges115 silver badges160 bronze badges asked Sep 28, 2013 at 2:03 user2524908user2524908 8714 gold badges19 silver badges46 bronze badges 6
  • Without knowing the context of your problem, I'd say it's better simply edit whatever code is making div visible and have that "process the next line". – Brad Peabody Commented Sep 28, 2013 at 2:08
  • Deferred.done() – Brian Commented Sep 28, 2013 at 2:08
  • Unfortunately its not that sample, I am extending the functionality of a very large BI application. The javascript which controls this div is very hidden and all the methods are pretty unreadable. At some point the divs viability is changed to visible, I can invoke a custom method at this time using the above code. I need to wait until the div is no longer visible to invoke my next line – user2524908 Commented Sep 28, 2013 at 2:10
  • how are you hiding that "div" ? if you are using jQuery to hide it then you can define callback that are execute when hiding its done, see api.jquery./hide/‎ – Eugen Halca Commented Sep 28, 2013 at 2:11
  • I am not hiding the div. There is some native javascript code which is performing that action. I cannot extend the functionality of that method unfortunately. But the native javascript is toggling the visibility – user2524908 Commented Sep 28, 2013 at 2:14
 |  Show 1 more ment

3 Answers 3

Reset to default 8

You can use the setTimeout function to poll the display status of the div. This implementation checks to see if the div is invisible every 1/2 second, once the div is no longer visible, execute some code. In my example we show another div, but you could easily call a function or do whatever.

http://jsfiddle/vHmq6/1/

Script

$(function() {
  setTimeout(function() {
    $("#hideThis").hide();    
  }, 3000);
  pollVisibility();

  function pollVisibility() {
      if (!$("#hideThis").is(":visible")) {
          // call a function here, or do whatever now that the div is not visible
          $("#thenShowThis").show();
      } else {
          setTimeout(pollVisibility, 500);
      }
  }
}

Html

<div id='hideThis' style="display:block">
  The other thing happens when this is no longer visible in about 3s</div>

<div id='thenShowThis' style="display:none">Hi There</div> 

If your code is running in a modern browser you could always use the MutationObserver object and fallback on polling with setInterval or setTimeout when it's not supported.

There seems to be a polyfill as well, however I have never tried it and it's the first time I have a look at the project.

FIDDLE

var div = document.getElementById('test'),
    divDisplay = div.style.display,
    observer = new MutationObserver(function () {
        var currentDisplay = div.style.display;

        if (divDisplay !== currentDisplay) {
            console.log('new display is ' + (divDisplay = currentDisplay));
        }
    });

//observe changes
observer.observe(div, { attributes: true });

div.style.display = 'none';

setTimeout(function () {
    div.style.display = 'block';
}, 500);

However an even better alternative in my opinion would be to add an interceptor to third-party function that's hiding the div, if possible.

E.g

var hideImportantElement = function () {
    //hide logic
};

//intercept
hideImportantElement = (function (fn) {
    return function () {
        fn.apply(this, arguments);
        console.log('element was hidden');
    };
})(hideImportantElement);

I used this approach to wait for an element to disappear so I can execute the other functions after that.

Let's say doTheRestOfTheStuff(parameters) function should only be called after the element with ID the_Element_ID disappears, we can use,

var existCondition = setInterval(function() {
 if ($('#the_Element_ID').length <= 0) {
    console.log("Exists!");
    clearInterval(existCondition);
    doTheRestOfTheStuff(parameters);
 }
}, 100); // check every 100ms
发布评论

评论列表(0)

  1. 暂无评论