【问题标题】:Two functions synchronously run in jqueryjquery中两个函数同步运行
【发布时间】:2012-11-28 19:49:41
【问题描述】:

我编写了一个脚本来模拟图像的淡入淡出并导致文本发生变化。这是现在的代码:

var teller = 0;
var html = new Array();
var link = new Array();
html[0] = "Some text";
link[0] = "link1.html";
html[1] = "Some other text";
link[1] = "link2.html";
function beeldwissel() {
    tekstwissel();
    var $actief = $('#overvloeier IMG.actief');
    if ( $actief.length == 0 ) $actief = $('#overvloeier IMG:last');
    var $next =  $actief.next().length ? $actief.next() : $('#overvloeier IMG:first');
    $actief.addClass('laatst-actief');
    $next.css({opacity: 0.0})
        .addClass('actief')
        .animate({opacity: 1.0}, 1000, function() {
            $actief.removeClass('actief laatst-actief');});
        }

$(function() {setInterval( "beeldwissel()", 5000 );}); // 5000 = pauze van 5 seconden

function tekstwissel() {
    teller++;
    if (teller >= html.length) {
        teller = 0;
    }   
    document.getElementById('tel').value = teller;
    document.getElementById('tekst').innerHTML = html[teller];
}

function doLink() {
    var tel = document.getElementById('tel').value;
    window.open(link[tel],'_blank');
}

如果我改变焦点而不是文本的变化和图像的变化不再同步......

为了避免这个问题,我需要做些什么改变?

【问题讨论】:

    标签: jquery image function setinterval synchronize


    【解决方案1】:

    如果没有异步,这两个函数不能同时运行。您可以做的最好的事情是使用两个超时设置的时间非常短:

    setTimeout(function() { /* code... */ },1);
    setTimeout(function() { /* code... */ },1);
    

    这样两个函数同时执行。

    但是,在您的情况下,如果您只使用一个函数或让第一个函数调用另一个函数,会不会更简单:

    function trigger() {
         // Do image fading here...
         // Text manipulation here...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 2018-01-09
      • 2017-04-11
      • 2017-07-26
      • 1970-01-01
      • 2021-09-23
      相关资源
      最近更新 更多