English 中文(简体)
我可以用 SetTeamout 重置一个卸载的函数吗?
原标题:can I reset an unbinded function with setTimeout?

天啊,我经常在这里... 不管怎么说,问题就在这里。我有一个不固定的功能, 我需要在一段时间后再回来(最好是当一个视频停止播放的时候, 但现在我只要几秒钟就可以了)。

I found someone with a problem similar to mine here (on the jquery forums), and while I tried to apply the solution (the first response), it didn t work. here s what I tried to do
$( li ,  .thumbs ).bind( touchend click , playVideo);


$( li ,  .thumbs ).bind( touchend click , function() {
        //some code
        playVideo( $(this).index() );
        $("li", ".thumbs").unbind( click );
        setTimeout(function(){
            $( li ,  .thumbs ).bind( touchend click );
            },5000);
        });

为什么?

**EDIT okay, I got the bind to come back with the timeout, but for some reason it s not completing the function playVideo. It stops the video that s playing, but doesn t start the new one...
最佳回答
$( li ,  .thumbs ).bind( touchend click , onPlayClick);

function onPlayClick(){
    //some code
    playVideo( $(this).index() );
    $("li", ".thumbs").unbind( click , onPlayClick);
    setTimeout(function(){
        $( li ,  .thumbs ).bind( click , onPlayClick);
    },5000);
}

Also, if you re using jQuery 1.7+, the on() / off() are preferred. Example:

$( li ,  .thumbs ).on( click , onPlayClick);
$( li ,  .thumbs ).off( click , onPlayClick);
问题回答

这应该管用

$( li ,  .thumbs ).bind( touchend click , playVideo);    
$( li ,  .thumbs ).bind( touchend click , function() {
    //some code
    playVideo( $(this).index() );
    setTimeout(function(){
        $( li ,  .thumbs ).unbind().bind( touchend click ,playVideo);
        },5000);
    });

因为在此情况下, 每次设定暂停时间时, 您都不得不解开先前的绑定功能, 然后绑定。 另外, 在绑定的同时, 还必须有回溯功能 。

Or You can trigger Click instead of binding if you know where to click.
For Eg if class thumbs is to be click. Theen

  $( .thumbs ).trigger( click );   

在您设置的TetTimeout中,您不会将元素绑定到函数中。添加回传功能,问题就会得到解决。

setTimeout(function(){
    $( li ,  .thumbs ).bind( touchend click , playVideo);  // added callback
},5000);

记住,当您重新组合事件时,您必须明确传递一个函数,在事件触发时可以调用该函数,即使您先前已经登记过事件,并对该元素事件进行了解套。

此外,要记住,设置超时和设置跨时回馈功能具有全球范围,这意味着它们具有“窗口”层面的可见度。 您需要确保功能“ Video ” 在全球范围的定义, 在设置超时中可见。





相关问题
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.

热门标签