English 中文(简体)
观察套 时间选择,只有一人在时间运行
原标题:Watching setTimeout loops so that only one is running at a time

I m 在jQuery中设立内容轮换制。 共有5个项目。 项目1 Repeat.

足够简单。 采用准时安排,我可以召集一系列职能,创造机会,无限期地重复这一进程。

我现在想补充一点能力,即通过点击导航元件,以直接跳跃到其中一个内容项目,在任何时候打断轮机。

最初,我开始走上不断铺设变量的道路(每半半小时),以便检查是否点击了导航元件,如果是,则放弃航道,然后根据被点击的项目重新启动航道。

我面临的挑战是,如何通过时间推算变量。 解决办法是 into式封闭式......这比我的头部稍多,但肯定是我需要再研究的东西。

然而,在这项工作过程中,我提出了一种选择,实际上似乎是更好的业绩(至少在理论上)。 我这里有一个样本:

rel=“nofollow noreferer”>http://jsbin.com/uxupi/14

(它使用青 con素,因此有火力。)

样本:

$(document).ready(function(){

var loopCount = 0;

$( p#hello ).click(function(){
  loopCount++;
  doThatThing(loopCount);
})

function doThatOtherThing(currentLoopCount) {  
console.log( doThatOtherThing- +currentLoopCount);
if(currentLoopCount==loopCount){
  setTimeout(function(){doThatThing(currentLoopCount)},5000)
 }
}  


function doThatThing(currentLoopCount) {
 console.log( doThatThing- +currentLoopCount);
 if(currentLoopCount==loopCount){
   setTimeout(function(){doThatOtherThing(currentLoopCount)},5000);
 }
}

}) 

逻辑是,每个点击触发元件都会把空投分解成一个等于全球变量当前价值的变量。 这种变数在摊位的功能之间回落。

每个点点击触发器还使全球变数增加,使后方的通话具有独特的当地变量。

然后,在休息室内,在每个休息室的下一个步骤之前,它检查了它是否仍然符合全球变量。 如果不是的话,它知道已经启动新的通道,以便它结束现有的通道。

对这一点的思考? 有效解决办法? 更好的选择? 洞穴? 风险?

最新资料:

I m 采用下文通过明确时间选择提出的约翰建议。

然而,我可以让它工作。 逻辑是:

var slideNumber = 0; 

var timeout = null;


function startLoop(slideNumber) {
    //... code is here to do stuff here to set up the slide based on slideNumber...
    slideFadeIn()
}

function continueCheck() {
    if (timeout != null) {
        // cancel the scheduled task.
        clearTimeout(timeout);
        timeout = null;
        return false;
    } else {
        return true;
    }
};

function slideFadeIn() {
    if (continueCheck){
        // a new loop hasn t been called yet so proceed...

        $mySlide.fadeIn(fade, function() {
            timeout = setTimeout(slideFadeOut,display);
        }); 
    }       
};


function slideFadeOut() {
    if (continueCheck){
        // a new loop hasn t been called yet so proceed...

        slideNumber=slideNumber+1;

        $mySlide.fadeOut(fade, function() {
            //... code is here to check if I m on the last slide and reset to #1...
            timeout = setTimeout(function(){startLoop(slideNumber)},100);
        });

     }   
};


startLoop(slideNumber);

The above kicks of the looping.

我有导航物品,在被点击时,我想停止上述飞行,然后重新开始滑坡:

$(myNav).click(function(){
    clearTimeout(timeout);
    timeout = null;
    startLoop(thisItem);
})

如果我从点击事件开始Loop......的话,它实际上就停止了最初的 lo。 然而,如果我离开最后一行,那实际上就没有停止最初的 lo。 为什么? 发生的情况是,这两个地方的运行似乎在一段时期内同时进行。

因此,当我点击我的航行时,即叫停了,这清楚了。

问题回答

您应做的是,为打断轮机而用<条码> 准时<>/代码>回收的操作,并用<条码>明了。

var timeout = null;

function doThatThing() {
    /* Do that thing. */

    // Schedule next call.
    timeout = setTimeout(doThatOtherThing, 5000);
}

function doThatOtherThing() {
    /* Do that other thing. */

    // Schedule next call.
    timeout = setTimeout(doThatThing, 5000);
}

function interruptThings() {
    if (timeout != null) {
        // Never mind, cancel the scheduled task.
        clearTimeout(timeout);
        timeout = null;
    }
}

当一个导航元件被点击时,只打上了<条码>,打上了<条码>。 唯一方面是,它将立即生效,而不需要进行任何投票或任何其他复杂的事情。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签