Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有更好的方法来实现这个jQuery setInterval?_Javascript_Jquery_Tinymce - Fatal编程技术网

Javascript 有没有更好的方法来实现这个jQuery setInterval?

Javascript 有没有更好的方法来实现这个jQuery setInterval?,javascript,jquery,tinymce,Javascript,Jquery,Tinymce,我想知道这是否是一个更好的代码替代品,以使其看起来不那么黑客: active_tiny_editor = setInterval(function () { if(tinyMCE.activeEditor!==null){ my_object.switch_tabs('tmce'); clearInterval(active_tiny_editor); } }, 200); 理想情况下,问题在于重新加载页面后,Wordpress可视化编辑器将消失。我需要切换回文本模式,然后再次

我想知道这是否是一个更好的代码替代品,以使其看起来不那么黑客:

active_tiny_editor = setInterval(function () {
if(tinyMCE.activeEditor!==null){
    my_object.switch_tabs('tmce');
    clearInterval(active_tiny_editor);
}
}, 200);
理想情况下,问题在于重新加载页面后,Wordpress可视化编辑器将消失。我需要切换回文本模式,然后再次切换到可视化编辑器以查看内容

在第一次加载之后,页面上似乎没有正确初始化某些内容,在切换编辑模式时,这些内容得到了修复

如果你有什么想法,请告诉我。谢谢

更新:

代码如下:

            // select active editor tab
            switch(bootstrap_config_object.editor_active_tab)
            {
                case 0:
                    bootstrap_object.switch_tabs('html');                        
                    break;
                case 1:
                    if(bootstrap_config_object.tmce_editor_status==1){                          
                        active_tiny_editor = setInterval(function () {                              
                            if(tinyMCE.activeEditor!==null){                                    
                                bootstrap_object.switch_tabs('tmce');
                                clearInterval(active_tiny_editor);                                    
                            }
                        }, 200);
                    }else{                          
                        bootstrap_object.switch_tabs('html');                            
                    }
                    break;
                case 2:                     
                    bootstrap_object.switch_tabs('syntax');                        
                    myCodeMirror.refresh();
                    break;
            }
然后这是包含切换选项卡逻辑的引导\u对象:

switch_tabs: function(do_active) {// switch active tabs   
    switch(do_active)
    {
        case 'html':                
            jQuery('#content-html').addClass('switch-html');
            jQuery('#content-bootstrap').removeClass('switch-bootstrap');
            jQuery('#wrap-code-bootstrap, #codemirror-insert-media').addClass('element-hide');
            jQuery('#content, #ed_toolbar, .insert-media, #content-resize-handle').removeClass('element-hide');
            jQuery('#wp-content-wrap').addClass('html-active').removeClass('syntax-active').removeClass('tmce-active');

            jQuery('#content').show();
            jQuery('#content_parent, .CodeMirror').hide();               

            if(bootstrap_config_object.codemirror_status==1){                   
                bootstrap_object.replase_shortcode_add_method('normal');
            }
            break;
        case 'tmce':                
                            active_tiny_editor = setInterval(function () {
                            if(tinyMCE.activeEditor!==null){
                                jQuery('#content_parent').show();
                                clearInterval(active_tiny_editor);
                            }
                        }, 200);                
            jQuery('#content-tmce').addClass('switch-tmce');
            jQuery('#content-bootstrap').removeClass('switch-bootstrap');
            jQuery('#wrap-code-bootstrap, #content, #ed_toolbar, #codemirror-insert-media').addClass('element-hide');
            jQuery('#content_parent, .insert-media, #content-resize-handle').removeClass('element-hide');
            jQuery('#wp-content-wrap').addClass('tmce-active').removeClass('syntax-active').removeClass('html-active');

            jQuery('#content_parent').show();
            jQuery('.CodeMirror, #content').hide();

            if(bootstrap_config_object.codemirror_status==1){                   
                bootstrap_object.replase_shortcode_add_method('normal');
            }
            break;
        case 'syntax':              
            jQuery('#content-bootstrap').addClass('switch-bootstrap');
            jQuery('.wp-switch-editor').removeClass('switch-tmce switch-html');
            jQuery('#content_parent, #content, #ed_toolbar, .insert-media, #content-resize-handle').addClass('element-hide');
            jQuery('#wrap-code-bootstrap, #codemirror-insert-media').removeClass('element-hide');
            jQuery('#wp-content-wrap').addClass('syntax-active').removeClass('tmce-active').removeClass('html-active');

            jQuery('.CodeMirror').show();
            jQuery('#content_parent, #content').hide();

            // resize codemirror 
            wp_settings = bootstrap_object.URLToArray(jQuery.cookie("wp-settings-"+bootstrap_config_object.user_id));
            if(wp_settings){                    
                jQuery('.CodeMirror').css('height', parseInt(wp_settings['ed_size'])+30+'px');
            }
            tinyMCE.execCommand('mceRemoveControl', false, 'content');

            if(bootstrap_config_object.codemirror_status==1){
                bootstrap_object.replase_shortcode_add_method('syntax');                    
            }
            break;
    }
    // set active tab        
    jQuery('#editor-active-tab').val(do_active);
功能:

我在WP编辑器中有三个编辑选项卡,即WP中的传统视觉和文本编辑器。然后,我添加了一个名为“Syntax”的新编辑器,该编辑器将转换为使用代码镜像显示语法HTML。我需要它们在使用它和添加短代码等方面保持一致和稳定。

看起来更好吗

function checknSwitch() {  
    if(tinyMCE.activeEditor!==null) my_object.switch_tabs('tmce');
}

setTimeout(checknSwitch, 200);

tinyMCE.activeEditor
在用户单击tinyMCE编辑器实例时设置。这里的问题是重新加载页面只会删除html节点,而不会删除编辑器本身。 在初始化具有相同id的编辑器之前,首先需要正确关闭该tinymce实例。请参阅此SO线程:

顺便说一句,如果可能的话,最好使用
tinymce.get('your_editor_id')
而不是使用
tinymce.activeEditor
tinymce.get('your\u editor\u id')
在初始化时始终设置/可用


应该根本不需要设置间隔。

如果只需要一次,只需使用设置超时即可,因此您不需要分配和清除它。我想我在代码中多次使用它们。谢谢Thariama,这非常有趣。我不知道这一个..我可以在其他线程中发布我的详细代码,以便您可以准确地看到它吗?然后我会把你的答案标记为正确的。干杯。用你的代码更新你自己的问题可能更好。我已经用代码更新了我的问题。我希望现在一切都清楚了。我相信当其他插件在编辑器中有它的短代码按钮时,黑客代码会有一些副作用(在WP中,就像当你在编辑器按钮中点击它时,短代码会被添加到编辑器中)。当我点击它们时,它们不工作,也没有添加到编辑器中。谢谢我相信有更好的解决办法。我感谢你的帮助。Cheers.hmm,如果设置了tinyMCE.activeEditor,您想切换选项卡吗?是的。我已经尝试添加tinyMCE.execCommand('mceRemoveControl',true,'content');就在切换选项卡之前,但切换机制似乎不稳定。相当于设置间隔&清除间隔,只有在
tinyMCE.activeEditor==空
。您的解决方案甚至可以运行一次
tinyMCE.activeEditor==null
。我已经发布了我的更新代码。