-1

we are using require.js in our project and we need to override the setTimeout in line 1802, this is the code which we need to set to 0 or ignore somehow this setTimeout at all(I mean ru over it) ,the problem that if I change it in the open source code explicit when I change version the code will be lost,how should I override this setTimout from outside only for the require.js file and keep it as long as I use this lib, is it possible to do it in elegant way in JS?

/**
 * Execute something after the current tick
 * of the event loop. Override for other envs
 * that have a better solution than setTimeout.
 * @param  {Function} fn function to execute later.
 */
req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) {
    setTimeout(fn, 4);
} : function (fn) { fn(); };

This is the link for the require.js open source in Git https://github.com/jrburke/requirejs/blob/master/require.js line 1802

  • Why do you need to change it to 0, instead of 4ms? – Rowland Shaw Jan 02 '16 at 20:20
  • @RowlandShaw - HI Rowland this is the reason http://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs –  Jan 02 '16 at 20:29
  • You'd still be limited to one call per second, per the answer there – Rowland Shaw Jan 02 '16 at 20:44
  • @RowlandShaw so do you have any other better idea how to skip this 1 call per sec issue with require.js? –  Jan 02 '16 at 20:55
  • This is a browser restriction, so no, other than not supporting use in the background, for that browser – Rowland Shaw Jan 02 '16 at 20:57
  • @RowlandShaw - thanks but we cannot :) since we are opening new window the the current tab become in-active... –  Jan 02 '16 at 21:14

1 Answers1

2

require object is exposed as global variable so you can override its methods like this:

window.require.nextTick = function(fn) {
  setTimeout(fn, 0);
}

but I can't think of any good reason to do this.


This, however, will not solve your problem since setTimeout calls will be limited by the browser anyway. Better option will be to simply call the function:

window.require.nextTick = function(fn) { fn(); };

You can see in require's sources that original nextTick function can have two implementations depending on setTimeout availability - one with setTimeout and one with function call only when setTimeout is undefined (last line of the snippet you pasted). This is why I think overriding this function like above shouldn't cause any problems.

xersiee
  • 4,432
  • 1
  • 14
  • 23
  • Thanks 1+ , so I need to use it before I use the require.js and this will override the nextTick?The reason for it is this http://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs assume that I want to override it I mean ignore/ remove the setTimeout code at all I just need to put your code without the setTimeout? –  Jan 02 '16 at 20:32
  • Yes, I haven't tested it but it should work. Even function's documentation says that it can be overriden. However, your implementation will introduce some inconsistency because function will no longer execute code in 'next tick' but immediately. – xersiee Jan 02 '16 at 20:42
  • Thanks xersiee , you said the following if I remove setTiemout at all " function will no longer execute code in 'next tick' but immediately", and which problems this can be raise? –  Jan 02 '16 at 20:53
  • It probably won't cause any problems, you can see in their code that they simply call provided function if `setTimeout` is not supported by the environment. Unless your code uses this function in a weird way. My point was only that it's ugly, not very dangerous:) – xersiee Jan 02 '16 at 21:04
  • Thanks but do you mean for removing the setTimeout at all like following ? window.require.nextTick = function(fn) { } or do it exactly like the code you provided ? and last question :) you write " code that they simply call provided function if setTimeout is not supported by the environment" ,which line it is? Thanks!! –  Jan 02 '16 at 21:13
  • Thanks:) ,ive additional question on the same topic please see if you can give some hints, Thanks in advance!http://stackoverflow.com/questions/34587078/override-settimeout-in-require-js –  Jan 04 '16 at 08:21