首页 \ 问答 \ SetTimeOut问题(SetTimeOut problems)

SetTimeOut问题(SetTimeOut problems)

我正在处理一个项目的问题。 我不是一个JS专家,所以如果这是一个愚蠢的问题,我很抱歉。

我有2个视频在彼此的顶部。 当我调用“开关”功能时,它会播放“静态”淡入淡出效果,然后切换两个视频的可见性。

一切正常,但是当我单击按钮以非常快速地调用该函数时,“静态”效果会变得粗糙并且一切都开始出错。

这是我正在使用的代码。 它会切换类名以隐藏视频。

function Switch(){
    if(videoNieuw.className == "show"){
        playNoise(1280, 720);
        btnswitch.className="controls now";
        setTimeout(function(){
            videoNieuw.className="hide";
            videoOud.className = "show";
        }, 500);

    }else if(videoOud.className = "show"){
        playNoise(1280, 720);
        btnswitch.className="controls then";
        setTimeout(function(){
            videoOud.className = "hide";
            videoNieuw.className="show";
        }, 500);
    }
}

setTimeout是这样的“静态”淡入淡出有时间淡化一点,让事情看起来更顺畅。

有没有其他方法,所以我可以把它放在没有故障的情况下?


I have a problem with a project i'm working on. I'm not in any way a JS expert so i'm sorry if this is a stupid thing to ask.

I have 2 video's that are on top of each other. when i call the function "switch" it plays a "static" fade effect and then switches the visability of both of the videos.

it all works fine, but when i click the button to call the function very fast, the "static" effect gliches out and everything starts to bug out.

here is the code that i am using. it switches the class name to hide the video.

function Switch(){
    if(videoNieuw.className == "show"){
        playNoise(1280, 720);
        btnswitch.className="controls now";
        setTimeout(function(){
            videoNieuw.className="hide";
            videoOud.className = "show";
        }, 500);

    }else if(videoOud.className = "show"){
        playNoise(1280, 720);
        btnswitch.className="controls then";
        setTimeout(function(){
            videoOud.className = "hide";
            videoNieuw.className="show";
        }, 500);
    }
}

The setTimeout is so the "static" fade has the time to fade a little bit and make things look smoother.

is there an alternative way so i can put this up without glitching?


原文:
更新时间:2021-09-05 11:09

最满意答案

好吧,如果你真的需要在UserControl公开SelectedItem ,为什么不用这样的属性扩展它呢?

例如

public class MyUserControl : UserControl
{
    private static readonly SomeType SelectedItemProperty = 
        DependencyProperty.Register("SelectedItem", typeof(SomeType), typeof(MyUserControl));

    public SomeType SelectedItem
    {
        get { return (SomeType)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }
}

现在,您可以将UserControl DataGridSelectedItem绑定到其SelectedItem属性。

<MyUserControl>
    <DataGrid SelectedItem="{Binding SelectedItem, 
              RelativeSource={RelativeSource FindAncestor, 
              AncestorType={x:Type MyUserControl}}" />
</MyUserControl>

现在,您只需要找到一种方法来访问TabItemSelectedItem属性。 但是我要把那部分留给你了。

请注意,这只是我的想法的一个例子,它可能包含一些小错误。


Well if you really need to expose the SelectedItem from within a UserControl why don't you extend it with such a property?

E.g.

public class MyUserControl : UserControl
{
    private static readonly SomeType SelectedItemProperty = 
        DependencyProperty.Register("SelectedItem", typeof(SomeType), typeof(MyUserControl));

    public SomeType SelectedItem
    {
        get { return (SomeType)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }
}

So now you can bind the SelectedItem of the DataGrid in the UserControl to its SelectedItem property.

<MyUserControl>
    <DataGrid SelectedItem="{Binding SelectedItem, 
              RelativeSource={RelativeSource FindAncestor, 
              AncestorType={x:Type MyUserControl}}" />
</MyUserControl>

Now you only have to find a way to access the SelectedItem property in the TabItem. But I'm leaving that part to you.

Please note, that this is only an illustration of my idea and it may contain some small errors.

相关问答

更多