2013-03-26 104 views
0

你好我找到一个特定的工作模式;模式如果失败1尝试2如果失败尝试3为nodejs

可以说IM寻找一个标题在页面的DOM

如果发现标题,然后把它放到VAR标题 如果VAR标题仍然是空的,然后尝试下一个功能 如果VAR标题仍然是空的,然后试了下功能

是有那么一个更好的办法

// Find Title 
output.title = $('title').text(); 

if (null(output.title)) { 
    output.title = second try 
}; 

if (null(output.title)) { 
    output.title = 3rd try 
}; 

etc ? 
+0

你可以利用的setInterval的'()'和 – Gntem 2013-03-26 10:12:27

回答

1

我的版本使得它更可扩展性和逻辑性。 使用数组和while循环(使用异步模块):

var functions = [function1, function2, function3] 
var i = 0 
var output.title // to deal with scope issue of output.title only being defined inside whilst. Could have put output.title as argument for callback 
async.whilst(
    function() { return i < functions.length && !output.title }, 
    function (callback) { 
      output.title = functions[i] 
      i++ 
      callback() 
}, function() { 
    if (output.title) { 
     //have title 
    } 
    else { 
     // no title was found 
    } 
}) 
+0

感谢检查每'X'毫秒更改async.js同时是寻找什么即时通讯对于。从来没有听说过。 – user1780413 2013-03-26 10:51:37

+0

没问题,这是一个很棒的小模块,可以做你想要的一切。 – Niall 2013-03-26 11:07:08

相关问题